Python List extend VS append
In this short guide, we will discuss the differences between the extend and append in Python lists with examples.
Both extend
and append
are used to add elements to a list in Python but they have different uses.
- append() Method
- extend() Method
- append() VS extend() Comparison
- append() VS extend() Examples
- Conclusion
Table Of Contents
Python append() Method
The append() is a python list method that adds an element to the end of a list.
The syntax of append() method is:
list.append(element)
The append() method adds the entire element as it is, making it a single element of the list.
a = [1, 2, 3]
# append 4
a.append(4)
print(a)
# append "hello"
a.append("hello")
print(a)
Output:
[1, 2, 3, 4] [1, 2, 3, 4, 'hello']
Appending a collection of items:
a = [1, 2, 3]
# append [4, 5]
a.append([4, 5])
print(a)
# append ('a', 'b')
a.append(('a', 'b'))
print(a)
Output:
[1, 2, 3, [4, 5]] [1, 2, 3, [4, 5], ('a', 'b')]
You can see in the output above appended list is added as a single element of the list the same for the tuple.
Python extend() Method
The extend() is also a python list method that adds an element to the end of a list but it adds the element by extending the iterable object and not as a single element.
The syntax of extend() method is:
list.extend(element)
The extend() method extends the list by appending all the elements of the iterable object as individual elements of the list.
a = [1, 2, 3]
# extend [4, 5]
a.extend([4, 5])
print(a)
Output:
[1, 2, 3, 4, 5]
Extending string and tuple:
a = [1, 2, 3]
# extend 'hello'
a.extend('hello')
print(a)
# extend (0, 100)
a.extend((0, 100))
print(a)
Output:
[1, 2, 3, 'h', 'e', 'l', 'l', 'o'] [1, 2, 3, 'h', 'e', 'l', 'l', 'o', 0, 100]
Python List extend VS append Comparison
The following table shows the differences between extend() and append() in Python lists.
append() | extend() |
---|---|
Appends a single element to the end of the list. | The extend() method extends the list by taking element one by one from the iterable object and appending it to the list. |
Adding one element increases the size of the list by one whether it is a single element or a collection of elements. | Adding a collection of items increases the size of the list by the size of the collection. |
append() method can be used to add a collection of items to the end of the list. | The extend() can't be used to add a collection of items. |
Time complexity of append() is O(1). | Time complexity of extend() is O(n). |
append() vs extend() Examples
Here are a few examples to compare the extend() and append() output values.
Example 1: Passing list
# append
a = [1, 2, 3]
a.append([4, 5])
print(a) # [1, 2, 3, [4, 5]]
# extend
a = [1, 2, 3]
a.extend([4, 5])
print(a) # [1, 2, 3, 4, 5]
Example 2: Passing tuple
# append
a = [1, 2, 3]
a.append((4, 5))
print(a) # [1, 2, 3, (4, 5)]
# extend
a = [1, 2, 3]
a.extend((4, 5))
print(a) # [1, 2, 3, 4, 5]
Example 3: Passing string
# append
a = [1, 2, 3]
a.append('abc'))
print(a) # [1, 2, 3, 'abc']
# extend
a = [1, 2, 3]
a.extend('abc')
print(a) # [1, 2, 3, 'a', 'b', 'c']
Conclusion
Both append() and extend() methods are used to add elements to the end of the list but with different purposes. The extend() method performs better than the append() method when you use append method to add a collection of items to the end of the list.
Check out Rock Paper Scissors game in Python.