How to append element in the list using Python?

PythonServer Side ProgrammingProgramming

append()

There may arise some situations where we need to add or append an element at the end of a list. We’ll use append() method in Python which adds an item to the end of the list.

The length of the list increases by one.

Syntax

list.append(item)

The single parameter item is the item to be added at the end of the list. The item can be number, string, another list, dictionary etc.

Append element at the end of the list

We can append an element at the end of a list using the append() method. The element is appended and the size of the list is increased.

Example

 Live Demo

colors=["red","blue","black"]
print("List",colors)
print("Size of list", len(colors))
colors.append("white")
print("Updated list",colors)
print("Size of updated list",len(colors))

Output

List ['red', 'blue', 'black']
Size of list 3
Updated list ['red', 'blue', 'black', 'white']
Size of updated list 4

Appending another list at the end of the list

The append() method can also add another list to the end of the list.

Example

 Live Demo

colors=["red","blue","black"]
print("List",colors)
fruits=["apple","grapes","mango"]
colors.append(fruits)
print("Updated list",colors)

Output

List ['red', 'blue', 'black']
Updated list ['red', 'blue', 'black', ['apple', 'grapes', 'mango']]

extend()

The extend() method also adds elements at the end of the list. This is different from append(). The extend() method iterates over its argument and adds each element into the list. The length of the list increases by the length of elements of the argument.

Syntax

list.extend(iterable)

The iterable can be a string or another list.

Extend list with a string element

The string is an iterable. Thus, extending a list with a string will append all the characters of the string at the end of the list. The length of the list increases by the length of the string in the argument.

Example

 Live Demo

colors=["red","blue","black"]
print("List",colors)
print("Size of list", len(colors))
colors.extend("white")
print("Updated list",colors)
print("Size of updated list",len(colors))

Output

List ['red', 'blue', 'black']
Size of list 3
Updated list ['red', 'blue', 'black', 'w', 'h', 'i', 't', 'e']
Size of updated list 8

Extending list with another list

The list is iterable. Extending list with another list will append all the elements of the argument list at the end of the existing list. The size of the list increases by the size of the list in the argument.

Example

 Live Demo

colors=["red","blue","black"]
print("List",colors)
print("Size of list", len(colors))
colors2=["white","pink","orange"]
colors.extend(colors2)
print("Updated list",colors)
print("Size of updated list",len(colors))

Output

List ['red', 'blue', 'black']
Size of list 3
Updated list ['red', 'blue', 'black', 'white', 'pink', 'orange']
Size of updated list 6

append() vs extend()

  • The append() and extend() have different functionalities.

  • The append() method adds the argument as a single element whereas the extend() iterates over the argument and adds each element at the end of the list.

  • The append() method increases the length of the list by one whereas the extend() increases the length of the list by length of the argument element.

  • The append() has constant time complexity i.e. O(1) whereas extend() has time complexity O(k) where k is the length of the argument.

raja
Published on 10-Mar-2021 13:59:27
Advertisements