How to Append/Add Elements to a List in Python?

In Python, a list is a data structure that can be changed and has a sequence of specific elements. It is utilized to store the collection of data including strings, integers, tuples, etc. Each list’s element or item has a unique specified index that starts from “0”. Sometimes, users may want to add new elements to a specific list. Python has various built-in methods or functions that enable users to add desired elements to the end of the list or at a specific index in the list. 

Quick Outline

  1. How to Create/Make a List in Python?
  2. How to Append/Add Elements to a Python List?

How to Create/Make a List in Python?

To create/make a list in Python, the square brackets “[ ]” are used. Users need to place desired elements inside these square brackets “[ ]” and split them with commas “,”. The list can have various data types including string, float, or integers. 

Syntax

The basic syntax to create a string is given below:

List_name = [<item1>, <item2>, <item3>, <item4>, ...]

Note: Replace these “<item1>”, “<item2>”, etc. with the particular string or integer values.

Example 1: Creating a Simple List 

In this example, we are creating a simple list called “countries” with three items i.e. “Pakistan”, “India”, and “Kuwait” and printing it:

countries = ['Pakistan', 'India', 'Kuwait']

print('List:', countries)

The below output shows the created list: 

Example 2: Creating a List of Different Data Types

Here, we are creating a list named “list1” with elements of different data types, such as string, integer, float, etc. as seen below:

list1 = ["Hello", 9, "h", 3.14]

print('List:', list1)

The list of different data types has been created successfully: 

Now, let’s move ahead and see how to append or add elements to a Python list.

How to Append/Add Elements to a Python List?

Adding elements to a list means inserting an item in any index of the list. Users can insert elements at the start, middle, or end of the Python list. 

To append or add elements to a list in Python, the following methods can be used:

  • Method 1: Append/Add Elements to a Python List Using “append()” Method
  • Method 2: Append/Add Elements to a Python List Using “insert()” Method
  • Method 3: Append/Add Elements to a Python List Using “extend()” Method
  • Method 4: Append/Add Elements to a Python List Using List Concatenation 

Method 1: Append/Add Elements to a Python List Using “append()” Method

The “append()” method is a predefined function that adds/inserts a single item or element at the end of the already present list in Python. Users need to pass the desired item as a parameter to the “append()” method. The item can be any string, number, or list that will be added at the end of the specific Python list. 

Check out the following cases to see how to add different items/elements to a Python list by utilizing the “append()” method.

Case 1: Adding String to a List

In this case, we have a list called “countries” with three elements i.e. “Pakistan”, “India”, and “Kuwait”. We will add a new string element to the list using the “append()” method. For instance, we are adding an element “SriLanka” to the countries list and displaying it as seen below:

countries = ['Pakistan', 'India', 'Kuwait']
print('List:', countries)

countries.append('SriLanka')
print('\nUpdated countries list:', countries)

In the below output, it can be seen that the new element has been successfully added to the list’s end:

Case 2: Adding a Number to a List

In this case, we will add a specific number to the “countries” list using the “append()” method:

countries = ['Pakistan', 'India', 'Kuwait']
print('List:', countries)

countries.append('5')
print('\nUpdated countries list:', countries)

The append() method has added the specified number “5” to the “countries” list successfully:

Case 3: Adding List to a List

Users can also append/add a new list as an element to an already existing Python list using the “append()” method. For example, we have an existing “countries” list. We are creating a new list named “Ranks” with an integer data type and appending it to the “countries” list using the “append()” method:

countries = ['Pakistan', 'India', 'Kuwait']

Ranks = ['1', '7', '5']

countries.append(Ranks)
print('Updated List: ', countries)

According to the below output, the second list has been successfully appended to the first list as an element:

Method 2: Append/Add Elements to a Python List Using “insert()” Method

The “insert()” method adds/inserts an element or item at any specified index of the Python list. The index is the place where the new element should be appended. Users must pass the desired index and the new element as an argument to the “insert()” method. The element can be any string, number, or list that will be added to a particular index of the Python list. 

Look at the below cases to see how to add different items/elements to a Python list using the “insert()” method.

Case 1: Adding String to a List

In this case, we have a “countries” list with three elements. We will add a new string element to this list at a specific index using the “insert()” method. For example, we are adding a “Canada” element to the countries list at index “2”:

countries = ['Pakistan', 'India', 'Kuwait']
print('List:', countries)

countries.insert(2, 'Canada')
print('\nUpdated countries list: ', countries)

It can be observed that the new element has been added to the specified index as seen below:

Case 2: Adding a Number to a List

In this example, we will add a specific number to the “countries” list. For instance, we are adding the number “18” as an element to the list at index “3” using the “insert()” method:

countries = ['Pakistan', 'India', 'Kuwait']
print('List:', countries)

countries.insert(3, '18')
print('\nUpdated countries list:', countries)

The below output shows that the specified element has been appended to the given index:

Case 3: Adding List to a List

Users can also append/add a new list as an element to any index of an already present Python list using the “insert()” method. Here, we have an existing “countries” list. We are creating a new list named “Ranks” and appending it to the “countries” list at index “1” using the “insert()” method:

countries = ['Pakistan', 'India', 'Kuwait']

Ranks = ['1', '7', '5']

countries.insert(1, Ranks)
print('Updated List: ', countries)

This has efficiently appended the second list to the first list at the specified index:

Method 3: Append/Add Elements to a Python List Using “extend()” Method

The “extend()” method extends Python’s list by adding/appending iterable elements to it. It is used to append all the one list’s iterable elements to the end of another list. The list’s length increases by the item numbers present in the iterable. The iterable includes strings, tuples, lists, etc. 

Go through the following cases to see how to add different items/elements to a Python list with the help of the “extend()” method.

Case 1: Adding String to a List

In this case, we have a list called “countries” with three elements. We will use the “extend()” method to append a new string element to this list. For example, we are passing a string “UK” to append its elements to the end of the countries list:

countries = ['Pakistan', 'India', 'Kuwait']
print('List:', countries)

countries.extend('UK')
print('\nUpdated countries list: ', countries)

As you can see, the elements of the provided string have been added to the end of the list:

Case 2: Adding Numbers to a List

In this case, we will add particular numbers to the “countries” list. Here, we are passing “492” numbers to the “extend()” method to append its elements to the list’s end:

countries = ['Pakistan', 'India', 'Kuwait']
print('List:', countries)

countries.extend('492')
print('\nUpdated countries list: ', countries)

The below output shows that all the elements of the provided numbers have been appended to the end of the list:

Case 3: Adding List to a List

Users can also use the “extend()” method with the specific list to append all of its elements at the end of the existing Python list. Here, we have an existing “countries” list. We are creating a new list named “list2” and appending all of its elements to the end of the “countries” list using the “extend()” method:

countries = ['Pakistan', 'India', 'Kuwait']

list2 = ['China', 'US', 'UK', '8']

countries.extend(list2)
print('Updated List: ', countries)

This has successfully appended the second list’s elements to the end of the first list as seen below:

Method 4: Append/Add Elements to a Python List Using List Concatenation 

List concatenation uses the “+” operator to add/join multiple lists and make a new list. This can also be used to add/append one list’s elements to the end of another list’s elements. 

For instance, we have two lists i.e. “count_list1” and “count_list2”. We will add these lists using the “+” operator and make a new list:

count_list1 = ['Pakistan', 'India', 'Kuwait']

count_list2 = ['China', 'US', 'UK', '4']

newList = count_list1 + count_list2
print('Updated List: ', newList)

This has appended the elements of the second list to the end of the first list:

Note: Click on the provided link to access Google Colab Notebook. 

Conclusion

Python provides various built-in methods/functions to append or add elements to a list. Users can append a desired element to the end of the list using the “append()”, or “extend()” methods or the “+” operator. Moreover, the “insert()” method can also be used to append an element at any specific index of the list. This article has efficiently explained all the methods to add/append desired elements to a list in Python.