append() and extend() in Python program


In this tutorial, we are going to learn about the most common methods of a list i.e.., append() and extend(). Let's see them one by one.

append()

append() method is used to insert an element at the end of a list. The time complexity of append() method is O(1).

Syntax

list.append(element) -> element can be any data type from the list of data types.

Let's see some examples.

Example

# initializing a list
nums = [1, 2, 3, 4]
# displaying the list
print('----------------Before Appending-------------------')
print(nums)
print()
# appending an element to the nums
# 5 will be added at the end of the nums
nums.append(5)
# displaying the list
print('----------------After Appending-------------------')
print(nums)

Output

If you run the above program, you will get the following results.

----------------Before Appending-------------------
[1, 2, 3, 4]
----------------After Appending-------------------
[1, 2, 3, 4, 5]

Appending a list.

Example

# initializing a list
nums = [1, 2, 3, 4]
# displaying the list
print('----------------Before Appending-------------------')
print(nums)
print()
# appending an element to the nums
# 5 will be added at the end of the nums
nums.append([1, 2, 3, 4])
# displaying the list
print('----------------After Appending-------------------')
print(nums)

Output

If you run the above program, you will get the following results.

----------------Before Appending-------------------
[1, 2, 3, 4]
----------------After Appending-------------------
[1, 2, 3, 4, [1, 2, 3, 4]]

extend()

extend() method is used to prolong a list with an iterable. The time complexity of extend() method is O(n), where n is the length of the iterable.

Syntax

list.extend(iterable) -> extend method iterates over the iterable and appends all the elements to the list.

Let's see some examples.

Example

# initializing a list
nums = [1, 2, 3, 4]
# displaying the list
print('----------------Before Appending-------------------')
print(nums)
print()
# extending the list nums
# 5, 6, 7 will be added at the end of the nums
nums.extend([5, 6, 7])
# displaying the list
print('----------------After Appending-------------------')
print(nums)

Output

If you run the above program, you will get the following results.

----------------Before Appending-------------------
[1, 2, 3, 4]
----------------After Appending-------------------
[1, 2, 3, 4, 5, 6, 7]

What if you pass a string to the extend() method? Let's see.

Example

# initializing a list
nums = ['h', 'i']
# displaying the list
print('----------------Before Appending-------------------')
print(nums)
print()
# extending the list nums
# 5, 6, 7 will be added at the end of the nums
nums.extend('hello')
# displaying the list
print('----------------After Appending-------------------')
print(nums)

Output

If you run the above program, you will get the following results.

----------------Before Appending-------------------
['h', 'i']
----------------After Appending-------------------
['h', 'i', 'h', 'e', 'l', 'l', 'o']

Conclusion

I hope you enjoyed the tutorial. If you have any doubts regarding the tutorial, mention them in the comment section.

Updated on: 01-Nov-2019

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements