We have known that lists are one of the most fundamental data structures in Python programming language.
A list is an ordered collection of elements of any data type, such as integers, floats, strings, objects, or other lists.
In Python, a list is mutable data type, meaning we can update or modify elements in existing lists after creation. It provides flexibility nature that allows us to add, remove, or modify elements in an existing list.
Adding elements to a list is a very common operation in Python. There are several ways to add elements to a Python list. The method you will choose will depend on your specific requirements.
In this article, we will learn the different ways to add elements in a list in Python.
Ways to add Elements in a List in Python
Here are some of the most common ways to add elements to the Python list:
- Append method
- Insert method
- Extend method
- Concatenation operator
- List comprehension
Let’s understand all ways one by one with the help of examples.
Adding Elements in List using append() Function
Using append() function is the simplest and straightforward way to add an element to a list. This function appends an element at the end of an existing list. It does not return any value but modifies the original or existing list.
The general syntax to define append() method is as:
Here, list_name is the name of the list to which an element will be added. The append() method takes an element as an argument to be added at the end of the list.
Let’s write a Python program in which we will add an element at the end of the existing list using append() method.
# Creating a list containing three elements. fruit_list = ["Apple", "Orange", "Banana"] # Adding an element to end of a list using append() function. fruit_list.append("Mango") print("Modified fruit list:",fruit_list )
Output: Modified fruit list: ['Apple', 'Orange', 'Banana', 'Mango']
Let’s write a program in Python to add a list as an element to another list using append() method.
list1 = [10, 20, 30] list2 = [40, 50, 60] list1.append(list2) print(list1)
Output: [10, 20, 30, [40, 50, 60]]
In this example, we have created two lists named list1 and list2. Then, we have used the append() function to add list2 as an element to list1. Finally, we have printed the result as a list that contains four elements, including list2 as a nested list.
One thing to keep in mind when using the append() method is that it always adds the new element to the end of the existing list. If you want to add an element at a specific position in the list, use the insert() method instead.
Adding Elements using insert() Function
The insert() method adds a specified element or object at a specific index position in the list. It does not return any value, but inserts a specified element at the specified index position mentioned in its argument. The general syntax for insert() method is as:
This method takes two arguments: the first argument represents the index position where we want to insert the new element, and the second argument represents the new element to be inserted.
In the above syntax,
- list_name: The name of the list in which we will insert the element.
- index: It represents the position at which the element will insert. It must be an integer value that represents the index of the element.
- element: The element to be inserted at the position mentioned into the list.
my_list = [10, 20, 30, 40] my_list.insert(2, 50) print(my_list)
Output: [10, 20, 50, 30, 40]
In this example, we have first created a list named my_list, containing four elements of integer type. Then, we have used the insert() function to add the integer value 50 at index 2 into the list.
Because of insertion, an element 30 is shifted one position to the right. Finally, we have printed the contents of my_list on the console, which now includes the new added element.
We can also use the insert() method to insert a list as an element at a specific position into the another list. Look at the below example.
# Creating two lists containing each one four elements. list1 = [10, 20, 30, 40] list2 = [40, 50, 60, 80] # Call insert() method to insert list2 at index position 1 in list1. list1.insert(1, list2) print(list1)
Output: [10, [40, 50, 60, 80], 20, 30, 40]
Adding Elements using extend() Function
The extend() function adds multiple elements to the end of a list, It takes one argument, which is an iterable, such as list, tuple, string containing the elements to be added to the list. The extend() function added new elements at the end of the existing list.
The general syntax for extend() function is as:
In the above syntax,
- list_name: It is the name of the list to which the elements will add.
- iterable: An iterable containing the elements to be added to the list.
# Creating a list containing four elements. my_list = [1.1, 2.2, 3.3, 4.4] # Call extend() method to add elements at the end of list. my_list.extend([5.5, 6.6, 7.7]) print(my_list)
Output: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
In this example, we have first created a list named my_list having four elements. Then, we have used the extend() method to add the elements [5.5, 6.6, 7.7] to the end of the list. Finally, we have displayed the contents of my_list, on the console which now includes the new added elements.
We can also use the extend() method to add the elements of one list to another. Here’s an example of it.
# Creating two lists, each having three elements. list1 = ["a", "b", "c" ] list2 = ["d", "e", "f"] # Call extend() method to add a list of elements to another existing list. list1.extend(list2) print(list1)
Output: ['a', 'b', 'c', 'd', 'e', 'f']
In this example, we have created two lists, list1 and list2. We have invoked the extend() method to add the elements of list2 to the end of list1. The result is a list containing six elements.
Adding Elements using Concatenation Operator
We can also use the concatenation operator (+) to add two or more lists together. This technique creates a new list containing all the elements from the original lists.
For more detail, go to this tutorial: Concatenate Lists in Python
Adding Elements using List Comprehension
List comprehension is a concise approach to create lists in Python. We can also use it to add elements to a list. The general syntax for list comprehension is as:
new_list = [expression for element in iterable if condition]
In this syntax,
- new_list: The new list created by list comprehension.
- expression: The operation to be performed on each element in the iterable.
- element: The element to be iterated in the iterable.
- iterable: The original list or iterable.
- condition (optional): The conditional statement that determines whether an element is enclosed in the new list.
# Creating a list having four elements. my_list = [2, 4, 6, 8] cubed_list = [x**3 for x in my_list] print(cubed_list)
Output: [8, 64, 216, 512]
In this example, we have first created a list named cubed_list with four elements. Then, we have used list comprehension to create a new list named cubed_list that contains the cube of each element in my_list.
The expression x**3 performs the cube operation on each element in my_list, while the for loop iterates through each element in my_list. Finally, we printed the contents of cubed_list.
In this tutorial, you have learned about how to add elements in a list in Python with the help of many unique examples. Hope that you will have understood the basic concepts of all methods to add elements into the list.
Thanks for reading!!!