Write a python code to explain the difference between append () and extend () list method

Difference between Append, Extend and Insert in Python

Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects.

Lists are mutable, and hence, they can be altered even after their creation. Lists have various methods, the most commonly used list method are append(), insert(), extend(), etc. In this article, we are going to discuss the difference between append(), insert(), and, extend() method in Python lists.

Append

It adds an element at the end of the list. The argument passed in the append function is added as a single element at end of the list and the length of the list is increased by 1.

Syntax:

list_name.append(element)

The element can be a string, integer, tuple, or another list.



Example:




# python program to demonstrate

# working of append function

# assign list

l = ['geeks']

# use method

l.append('for')

l.append('geeks')

# display list

print(l)

Output:

['geeks', 'for', 'geeks']

Insert

This method can be used to insert a value at any desired position. It takes two arguments-element and the index at which the element has to be inserted.

Syntax:

list_name(index,element)

The element can be a string, object, or integer.

Example:




# python program to demonstrate

# working of insert function

# assign list

l = ['geeks', 'geeks']

# use method

l.insert(1, 'for')

# display list

print(l)

Output:

['geeks', 'for', 'geeks']

Extend

This method appends each element of the iterable (tuple, string, or list) to the end of the list and increases the length of the list by the number of elements of the iterable passed as an argument.

Syntax:

list_name.extend(iterable)

Example:




# python program to demonstrate

# working of extend function

# assign list

l = ['hello', 'welcome', 'to']

# use method

l.extend(['geeks', 'for', 'geeks'])

# display list

print(l)

Output:

['hello', 'welcome', 'to', 'geeks', 'for', 'geeks']

Difference between Append, Extend and Insert

append() insert() extend()
The element passed as an argument is appended to the end of the list The element passed as the argument can be inserted at any desired position by passing the index along with it as a parameter. Each element of the iterable passed as an argument gets appended to the end of the list.
An iterable passed as an argument appends without any change as a single element to the end of the list. An iterable passed as an argument appends without any change as a single element to the desired index of the list. An iterable passed as an argument will append each of its elements to the end of the list
Length of the list increases by 1 Length of the list increases by 1 Length of the list increases by the number of elements in the iterable.
Has a constant time complexity of O(1) Has a linear complexity of O(n). Has a time complexity of O(k) where k is the length of the iterable.

Comparing the methods in a single program:




# Python program to demonstrate

# comparison between the three methods

# assign lists

list_1 = [1, 2, 3]

list_2 = [1, 2, 3]

list_3 = [1, 2, 3]

a = [2, 3]

# use methods

list_1.append(a)

list_2.insert(3, a)

list_3.extend(a)

# display lists

print(list_1)

print(list_2)

print(list_3)

Output:

[1, 2, 3, [2, 3]] [1, 2, 3, [2, 3]] [1, 2, 3, 2, 3]

Write a python code to explain the difference between append () and extend () list method




Article Tags :

Difference Between

Python

python-list

python-list-functions

Practice Tags :

python-list

append() and extend() in Python

Append: Adds its argument as a single element to the end of a list. The length of the list increases by one.

syntax: # Adds an object (a number, a string or a # another list) at the end of my_list my_list.append(object)




my_list = ['geeks', 'for']

my_list.append('geeks')

print my_list

Output:

['geeks', 'for', 'geeks']

NOTE: A list is an object. If you append another list onto a list, the parameter list will be a single object at the end of the list.




my_list = ['geeks', 'for', 'geeks']

another_list = [6, 0, 4, 1]

my_list.append(another_list)

print my_list

Output:

['geeks', 'for', 'geeks', [6, 0, 4, 1]]



extend(): Iterates over its argument and adding each element to the list and extending the list. The length of the list increases by number of elements in it’s argument.

syntax: # Each element of an iterable gets appended # to my_list my_list.extend(iterable)




my_list = ['geeks', 'for']

another_list = [6, 0, 4, 1]

my_list.extend(another_list)

print my_list

Output:

['geeks', 'for', 6, 0, 4, 1]

NOTE: A string is an iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string.




my_list = ['geeks', 'for', 6, 0, 4, 1]

my_list.extend('geeks')

print my_list

Output:

['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']

Time Complexity:
Append has constant time complexity i.e.,O(1).
Extend has time complexity of O(k). Where k is the length of list which need to be added.

Write a python code to explain the difference between append () and extend () list method




Article Tags :

Python

python-list

python-list-functions

Practice Tags :

python-list

🔹 Welcome

If you want to learn how to work with .append() and .extend() and understand their differences, then you have come to the right place. They are powerful list methods that you will definitely use in your Python projects.

In this article, you will learn:

  • How and when to use the .append() method.
  • How and when to use the .extend() method.
  • Their main differences.

Let's begin. ✨

Difference between Append, Extend and Insert in Python

List: are like an array of dynamic size, declared in another programming language such as vector in C++ or Arraylist in Java. It is not necessary for a list to be homogeneous, and this is the main reason that makes it the most powerful tool in Python. A single list can contain different data types such as strings, integers, and objects.

As we know, lists are mutable, so they can be altered even after creating them. Lists has several methods, among which append(), insert(), extend() are most common ones.

In this tutorial, we will learn how append(), expend(), and insert() functions are different from each other in Python's lists.

Append() Function

The append() function is used for adding an element at the end of the list. The argument we pass in the append() function is added as a single element at the end of the list, and the length of the list will increase by 1.

Syntax

The "element_1" can be an integer, tuple, string or another list.

Example:

Output:

The added elements in the given list are: ['The', 'list_1', 'is', 'an', 'example']

Insert() Function

The insert() function is used for inserting the value at any desired position in the list. We have to pass two arguments in it; the first is for index where we want to inter the element, and the second is for element to be inserted.

Syntax

The "element_1" can be an integer, tuple, string, or object.

Example:

Output:

The inserted elements in the given list are: ['The', 'list_1', 'is', 'an', 'example']

Extend() Function

The extend() function is used for appending each element of the iterable (list, string, or tuple) to the end of the list. This will increase the length of the list by the number of elements of the iterable are passed as an argument.

Syntax

Example:

Output:

The extended elements in the given list are: ['The', 'list_1', 'is', 'an', 'example', 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't']

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))

Write a python code to explain the difference between append () and extend () list method
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.