Python add to list

In this article, we will understand how to add element to a list in python in following ways:
1. Using append() method.
2. Using extend() method.
3. Using insert() method.
4. Append to list with + operator.

1. Add to list with append()
Python list append() method is used to add an object to a list. This object may be an individual element or another object such as a list, tuple, dictionary or a string.
Note that append() will add the element to the end of the list. Below is an example that adds a new element to the list.

# list of numbers
numbers=[1,2,3]
print('Original List:', numbers)
numbers.append(4)
print('Modified List:', numbers)

Output is

Original List: [1,2,3]

Modified List: [1,2,3,4]

With append(), you can add another list as an element to the list, or add a tuple to a list. Example,

numbers=[1,2,3]
print('Original List:', numbers)
numbers.append([4,5])
print('Modified List:', numbers)
# print the type of new element
print(type(numbers(3))

Output is

Original List: [1,2,3]
Modified List: [1,2,3,[4,5]]

<class ‘list’>

You can see that the newly added element is of type list.
2. Add to list with insert()
Python list insert() function allows you to add an element at any position or index in a list.

insert() takes two arguments:
A. index before which the element will be inserted. Elements of python list have numeric indexes, beginning with 0(index of first element).
B. element to insert.

When an element is inserted at an index, all the elements after that index in the original list are shifted towards right.
Below is an example to add element to list at index 0(beginning), in the middle and at the end.

numbers=[1,2,3]
print ('Original List:', numbers)

# insert at beginning
numbers.insert(0, -1)
print ('Added at index 0:', numbers)

# insert in middle
numbers.insert(3, -2)
print ('Added in middle:', numbers)

# insert at end
numbers.insert(5, 4)
print ('Added at end:', numbers)

Output of above example is

Original List: [1, 2, 3]
Added at index 0: [-1, 1, 2, 3]
Added in middle: [-1, 1, 2, -2, 3]

Added at end: [-1, 1, 2, -2, 3, 4]

insert() modifies the original list, it does not create a new one.

To append or add an element to the end of list, calculate the length of the list with Python len() function and use it as the first argument to insert().

length=len(numbers)
numbers.insert(length, 4)

3. Add to list with extend()
Python list extend() function accepts an iterable as argument and adds all the elements of that iterable to the end of list.
Iterable means a structure that can be iterated such as a list, set, tuple, string etc.
So, extend() may be used to append a list to another list. Example,

numbers=[1,2,3]
print ('Original List:', numbers)
# append another list
numbers.extend([4,5])
print ('List with appended list :', numbers)

# append set
numbers.extend({6,7})
print ('List with appended set :', numbers)

# append tuple
numbers.extend((8,9))
print ('List with appended tuple :', numbers)

Output is

Original List: [1, 2, 3]
List with appended list : [1, 2, 3, 4, 5]
List with appended set : [1, 2, 3, 4, 5, 6, 7]

List with appended tuple : [1, 2, 3, 4, 5, 6, 7, 8, 9]

With extend(), you can only add an iterable to a list. You can not add a single element to a list as with append() or insert().

Thus, when below code is executed

numbers=[1,2,3]
numbers.extend(4)

You will get

TypeError: ‘int’ object is not iterable

You can also pass a string to extend(), since a string is also an iterable. In this case, characters of the string will be added to the list as shown below.

numbers=[1,2,3]
print ('Original List:', numbers)
numbers.extend('etc')
print ('List with string added:', numbers)
Original List: [1, 2, 3]

List with string added : [1, 2, 3, ‘e’, ‘t’, ‘c’]

extend() modifies the original list.
4. Add to list with +
This method may also be used to append list to another list. Supply a + operator between the lists to be added.
It will return a new list with combined elements of all the lists. Original list will not be modified. Example,

numbers=[1,2,3]
print ('Original List:', numbers)
# append list to another list
more_numbers = numbers + [4,5]
print ('List with appended list :', more_numbers)

Output is

Original List: [1, 2, 3]

List with appended list : [1, 2, 3, 4, 5]

Note that with this method may only be used to concatenate a list to another list.
If you try to add a single element to a list or add a set or tuple to a list, TypeError will be raised.

Hope the article was useful.