Differentiating Append() Vs Extend() Method in Python

Introduction: Append() Vs Extend() in Python

One of the most useful data structures provided by python is List. You might think it would have been wonderful if you could add elements to the list after creating it. Well, then your wait is over here. In this article, we are going to learn some interesting methods to add elements to the list. Those methods are the following: – append and extend methods in Python. You will also get to know the differences between Append() Vs Extend().

Overview on List

The List is one of the most important tools in python. The list is similar to arrays, but the difference is that the list can hold any data type, such as an integer. Moreover, it is mutable, i.e., it can be modified, and because of this reason, we can use append and extend methods.

my_list = ['Banana', 'Apple', 'Peach', 1, 'Watermelon', 99 ]
print(type(my_list))
python extend vs append
List in python
Output: List

From the above code, you can see that a list can hold strings and integer data types. The index of the list starts from 0 and ends with n-1.

my_list = ['Banana', 'Apple', 'Peach', 1, 'Watermelon', 99 ]
print(my_list[0])
print(my_list[3])
OUTPUT:  Banana
                   1

Let’s come to the topic of append and extend methods. First of all, we will learn about the append method and how to implement it using python. We will also focus on its time complexity.

What is append method in python?

The append method in python will add an element to a list that is already created. It will not return a new list but modify the existing list by adding an element at the end of the list. Once you execute the list, the size of the list will increase by 1.

Syntax: list_name.append(element)

The append method takes only one element as a parameter and adds at the end of the list.

1. Appending element to a list in python

In the code below we will have a look at how to add element to a list.

#list
my_list = ['Banana', 'Apple', 'Peach', 1, 'Watermelon', 99 ]

#adding an element to a list
my_list.append('Orange')

#printing the appended list
print(my_list)

#length of the list
print(len(my_list))
OUTPUT: ['Banana', 'Apple', 'Peach', 1, 'Watermelon', 99, 'Orange' ]
                  7

Explanation of the code

  1. In the beginning, we are creating a list naming my_list having different elements.
  2. The parameter passed in the append method is ‘Orange.’
  3. After appending an element, we are now printing the updated list.
  4. A new element is added at the end of the my_list.
  5. After executing, the length of the list increases by one because of adding ‘Orange.’

2. Appending a list to a list in python

We can also separate a list by adding a string or an integer to a list. You might think what will be the size of the list we add a separate list. Let’s see the code.

#original list
my_list = ['Banana', 'Apple', 'Peach', 1, 'Watermelon', 99 ]

#create a new list
new_list = [1,2,3,4,5]

#append this list to our string_list
my_list.append(new_list)

# print the appended list
print(my_list)

#find the length of the original list
print(len(my_list))
OUTPUT: ['Banana', 'Apple', 'Peach', 1, 'Watermelon', 99,[1,2,3,4,5] ]
                  7

As you can see from the output itself, the new_list added to the original list counted as one.

Explanation of the code

  1. Created a list called my_list in which we can add a separate list.
  2. Also, I created a separate list named new_list.
  3. The parameter passed in the append method is the new_list.
  4. After appending a list, we are now printing the updated list, i.e., my_list.
  5. The new_list is added at the end of the my_list.
  6. After executing, the length of the list increases by one because of adding new_list.

Accessing the list

If you want to access the separate list added to the original list, you can do it the similar way we access an element.

print(my_list[6])
OUTPUT: [1,2,3,4,5]

If you want to access the elements from the separate list, you can similarly do that as you access elements from a 2-D Matrix.

print(my_list[6][2])
OUTPUT: 3

The first index, i.e., 6, is the last index of my_list, and the next index, i.e., 2, is the index of new_list.

Time complexity of append() method

The time that cost to append an element to an existing list in python is O(1).

What is extend method in python?

The extend method in python will add multiple elements to a list that is already created. It is much more advance than the append method. It will not return a new list but modify the existing list by adding an element at the end of the list. Once you execute the list, the size of the list will increase by the number of elements added to the list, i.e., n.

Syntax: list_name.extend(iterable)

Here iterable can be anything i.e., list, tuple, set etc.

1. Appending characters to a list in python

In this case if we pass a string as a parameter to the extend method we get characters.

#creating a list
my_list = [1, 10, 66, 2]

#extending
my_list.extend('Apple')

#printing the new list
print(my_list)

#length of the list
print(len(my_list))
OUTPUT:  [1, 10, 66, 2, 'A', 'p', 'p', 'l',  'e']
                   9

The string “Apple’ here is iterable, so if you extend a list with a string, you will append each character as you iterate over the string ‘Apple.’

Explanation of the code

  1. Created a list called my_list in which we can add a separate list.
  2. The parameter passed in the extend method is the ‘Apple.’
  3. After extending, we are now printing the updated list, i.e., my_list.
  4. The characters are added at the end of the my_list.
  5. After executing, the length of the list increases by five because of adding ‘Apple.’

2. Appending a tuple iterable to a list in python

Tuple iterable means that a tuple will be passed as the parameter in the extend method. Let’s see the code to understand the size of the updated list will be.

#create a list
fruits_list = ['Apple', 'Banana', 'Watermelon', 'Guava']

#tuple iterable
numbers = (1, 2, 5, 10)

#adding the elements in the tuple to the list 
fruits_list.extend(numbers)

#printing the updated list
print(fruits_list)

#length of the list
print(len(fruits_list))
OUTPUT: ['Apple', 'Banana', 'Watermelon', 'Guava', 1,  2,  5,  10 ]

Explanation of the code

  1. Created a list called fruits_list in which we can add a separate list.
  2. Also, I created a separate tuple named numbers.
  3. The parameter passed in the extend method is the numbers.
  4. After extending a tuple, we are now printing the updated list, i.e., fruits_list.
  5. The numbers are added at the end of the fruits_list.
  6. After executing, the length of the list increases by four because of adding numbers.

Time Complexity of extend()

The time complexity depends upon the number of elements that are added to the list. If there are n number of elements added to the list, the time complexity will be O(n). Here n can anything, i.e., 2,3,4…. and so on. For example, if 10 elements are added to the list, the time complexity will be O(10).

Append Vs Extend Method in Python

append()extend()
1. Adds only one element to the list. 1. Adds multiple elements, i.e., more than once to the list.
2. Length increases by 1.2. Length increases by n, i.e., the number of elements added.
3. Time complexity is constant, i.e., O(1).3. Time complexity is O(n), where n is the number of elements.
Append() Vs Extend()
Append vs extend in Python
Difference between append and extend methods

Also-Read

Conclusion: Append Vs Extend in Python

Python helps us to add elements into a list using the append and extend method. In this article, we presented append and extend methods, how to use them, when to use and the differences between them. The method you choose should be based on your requirements.

Questions Regarding Append Vs Extend Method in Python

Now, let’s solve few questions on the append and extend methods. It will help you to gain some confidence.

  1. What is the output of the following code snippet?
my_list1 = ['Sunita', 'Riya', 'Pearl', 100, 'Water', 99 ]
my_list1.append('Orange')
print(my_list1)

Your Answer:

0
Please leave a feedback on thisx

2. Find the output of the following code snippet

my_list1 = ['Sunita', 'Riya', 'Pearl', 100, 'Water', 99 ]
my_list1.extend('Litter')
print(my_list1)

Your Answer:

0
Please leave a feedback on thisx

3. Complete the following code to get the following as an output.

[10, 88, 59, 73, ‘Rakesh’]

nums = [10, 88, 59, 73]
x = ?
print(x)

Your Answer:

0
Please leave a feedback on thisx

4. Predict the output of the code given.

my_list1 = [10, 88, 59, 73]
x = {'a':10,'b':33}
my_list1.append(x)
print(my_list1)

Your Answer:

0
Please leave a feedback on thisx

5. Find the output of the following code snippet.

my_list1 = [10, 88, 59, 73]
x = {'a':10,'b':33}
my_list1.extend(x)
print(my_list1)

Your Answer:

0
Please leave a feedback on thisx

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments