Difference Between ‘+’ and ‘append’ in Python

Using ‘+’ operator to add an element in the list in Python:
Example:
sample_list =[]
n = 10
  
for i in range(n):
      
    # i refers to new element
    sample_list = sample_list+[i]
  
print(sample_list)

                    
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • The ‘+’ operator refers to the accessor method and does not modify the original list.
  • In this, sample_list doesn’t change itself. This type of addition of element in sample_list creates a new list from the elements in the two lists.
  • The assignment of sample_list to this new list updates PythonList object so it now refers to the new list.

Complexity to add n elements

Using .append() method i.e. an efficient approach:
Example:
sample_list =[]
n = 10
  
for i in range(n):
    # i refers to new element
    sample_list.append(i) 
  
print(sample_list)

                    
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1+.....(n-2) times...+1=O(n)
Note:
.append()

Graphical comparison between ‘+’ and ‘append’



Contact Us