Different Ways to add an item to a Python List


Different Ways to add an item to a Python List

In Python, We can divide the add operation into different scenario’s:

  1. Add a single element to the List: To add a single element to the list, we have the append() method and the insert() method.
  2. Combining/Concatenating List: For this, we can use the extend() method, + or += operator, or the list slicing.
Different Ways to add an item to a Python List

The append() method – Add an item to the end

The append() method is used, if we want to add any element to the end of the list. The append() method can be used in two ways:

  1. Add a single item at the end of the list: We will pass a single item in the parenthesis, and it will add that at the end of the list.
  2. Add a list or other data structure at the end: We can also use the append() method to append any other data structure like a list or tuple, etc at the end of the list. They will be added as a single item.

Let us have an example of both the ways:

Adding an item to the end of the list.

In [1]:
namelist = ["Bill", "Steve", "Tim"]
namelist.append("Tom")
print(namelist)
['Bill', 'Steve', 'Tim', 'Tom']

Adding a list to the end of the list.

In [2]:
namelist = ["Bill", "Steve", "Tim"]
namelist.append(["USA", "INDIA","UK"])
print(namelist)
['Bill', 'Steve', 'Tim', ['USA', 'INDIA', 'UK']]

Adding a tuple to the end of the list.

In [3]:
namelist = ["Bill", "Steve", "Tim"]
namelist.append(("USA", "INDIA","UK"))
print(namelist)
['Bill', 'Steve', 'Tim', ('USA', 'INDIA', 'UK')]

The insert() method – Add an item at a specific position

The insert() method is used, if we want to add a new element at the specific index position. It has two arguments, first is the index position and second is the element we wish to insert. Here, the index positions can be either positive (starting with 0) or negative (-1 means the last element).

Again, insert() method can be sued in two ways:

  1. To insert a single item at a specific index position.
  2. To insert any other data structure, like a list or tuple, etc, at the specific index position.

Let us have a look at various example:

Using Positive indexing to insert an item.

In [1]:
namelist = ["Bill", "Steve", "Tim", "Mike"]
namelist.insert(1, "Tom")
print(namelist)
['Bill', 'Tom', 'Steve', 'Tim', 'Mike']

Using Negative indexing to insert an item.

In [2]:
namelist = ["Bill", "Steve", "Tim", "Mike"]
namelist.insert(-1, "Tom")
print(namelist)
['Bill', 'Steve', 'Tim', 'Tom', 'Mike']

Inserting a tuple at a specific index position.

In [3]:
namelist = ["Bill", "Steve", "Tim", "Mike"]
namelist.insert(-1, ("Tom", "Elon" ))
print(namelist)
['Bill', 'Steve', 'Tim', ('Tom', 'Elon'), 'Mike']

The extend() method – Add elements from another list

  • The extend() method is used, if we like to merge the elements of two lists, i.e. if we want to append elements from one list to another.
  • With the help of the extend() method, we can append elements from other container data types (like tuple, set, dictionary) to a list.
  • If we try to provide an individual string to the extend() method, each character of the string will be added individually.

Let us look at the examples of all these three cases:

Appending items of a list with list.

In [1]:
namelist = ["Bill", "Steve", "Tim"]
namelist2 = ["Tom", "James"]
namelist.extend(namelist2)
print(namelist)
['Bill', 'Steve', 'Tim', 'Tom', 'James']

Appending items of tuple with list.

In [2]:
namelist = ["Bill", "Steve", "Tim"]
namelist2 = ("Tom", "James")
namelist.extend(namelist2)
print(namelist)
['Bill', 'Steve', 'Tim', 'Tom', 'James']

On using a string with the extend, each character of the string is added induvitually.

In [3]:
namelist = ["Bill", "Steve", "Tim"]
namelist.extend("Elon")
print(namelist)
['Bill', 'Steve', 'Tim', 'E', 'l', 'o', 'n']

Using the + and += Operator

  1. The + operator will concatenate two lists and will create the new list
  2. The += operator will concatenate the elements of one list to the other list.

Examples:

Using the + operator.

In [1]:
namelist = ["Bill", "Steve", "Tim"]
namelist2 = ["Tom", "James"]

final = namelist + namelist2
print(final)
['Bill', 'Steve', 'Tim', 'Tom', 'James']

Using the += operator.

In [2]:
namelist = ["Bill", "Steve", "Tim"]
namelist += ["Tom", "James"]

print(namelist)
['Bill', 'Steve', 'Tim', 'Tom', 'James']

Using the list slicing

Python is so powerful that we can add elements to the list using the list slicing operation, that too without losing any data. Let us have two ways by which list slicing can be used to add items to the list:

  1. Without replacing any existing item: If we are using this approach, then the start index and the end index have to remain the same, i.e. the position at which we want to add item(s).
  2. Replacing item at specific index position: In this approach, items from the start index and end index will be replaced with the new items.

Few Examples:

Without replacing any existing item.

In [1]:
namelist = ["Bill", "Steve", "Tim"]
namelist[1:1] = ["Tom", "James"]

print(namelist)
['Bill', 'Tom', 'James', 'Steve', 'Tim']

Replacing item at specific index position. Here Steve is replaced.

In [2]:
namelist = ["Bill", "Steve", "Tim"]
namelist[1:2] = ["Tom", "James", "Elon"]

print(namelist)
['Bill', 'Tom', 'James', 'Elon', 'Tim']

References:

  1. Add Elements to a List in Python
  2. Add an item to a list in Python (append, extend, insert)