How to Add Elements in List in Python?
In Python, a list is a dynamic array that can hold both homogeneous (all items of the same data type) and heterogeneous (items of different data types) data. The data stored in the list is arranged in order and identified by a unique number or position named an index.
In this tutorial, we will learn how to add Elements in List in Python using built-in functions. You can check out the Python list before learning to add elements.
How to add Elements in list in Python?
As mentioned earlier, a list in Python is a dynamic array, meaning that you can easily add new elements. You can add an element at any desired position or at the end of the list.
Methods to add elements in List in Python:
Sr. no | Methods | Description |
---|---|---|
1. | append() | Append method adds the object at the end of the list. |
2. | insert() | Insert method adds the object at the specified index. |
3. | extend() | Extend method appends elements from other iterables in python, like tuples, to the list. |
4. | List concatenation | + operator is used to concatenate multiple lists and create a new list. |
5. | Slicing | Adding another list or tuple using slice(). |
In the following section of this article, we will see all these methods regarding how to insert element in list in python.
Add an Item to the End: append()
append() method is used to add elements to the end of the list.
Output:
In this code, students.append() adds new element at the end of the original list students
Append takes only one argument, but we can add another list, heterogeneous elements, or tuples using append().
Output:
Above code illustrates how we can add lists, tuples, and elements with other data-type using the append() method.
Insert an Item at Specified Index: insert()
This method adds an element to the specified position on the list. The index of the list is used to specify the position where new elements are to be added.
Note: The index of the list in python starts from 0. The negative values like -1 mean one position before the end.
Output:
Here, in firts case, we can see that word.insert(3, "dog") inserts the string "dog" at 3rd index position. In the second case, words.insert(-2, "cricket") inserts the string "cricket" at 2nd index position from the end, the starting index from end being -0.
Combine Lists: extend(), + operator
We can add elements of one list to another using the extend() method and + operator. The extend() method and + operator is used to concatenate/combine one list into another.
extend() Method:
extend() method combines all elements of a list or tuple to the end of the original list.
Output:
The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number. list(range(5)) is used to create a list of the first five numbers. extend() function is used to add elements from a list to an existing list.
When a string is given as an argument to the extend() method, the string is split into its characters, and each character of the string is added to the original list one by one.
Let's an example of the same.
Output:
In this code list2.extend("Hello!") is used to add elements of string in the existing list. extend() method splits the string into characters and adds these characters to the existing list one by one.
List Concatenation using + operator:
We can combine elements of two lists in a single new list i.e. concatenate two lists using the + operator.
Let's see an example of adding elements in List in python using the + operator.
Output:
In the above code, lists even_numbers and odd_numbers are concatenated, and the results are displayed in a new list numbers using the + operator.
We can one list into the existing list with the help of += operators.
Output:
Here, the existing list fruits is appended with the elements of the new list fruits_new using +=. Operators += concatenates two lists, but it gives the result in the existing original string.
Adding Another List or Tuple in the Existing List Using slice()
Slicing in python: Python slice() function returns slice object. [start:end: step] is the syntax used to do slicing of any sequence in python. It returns a sliced object containing elements in the given range only. It takes starting index and ending index as arguments.
For eg., list[2:5] means to return the slice of the list starting from index 2 and ending at index 5.
Note: Ending index is not inclusive in slice(). It means the element at the ending index will not be included in the output.
Now let's see how to add elements in list in python using slicing.
Output:
In this code, numlist[1:1] adds the elements from the given list in the specified position, i.e., starting from 1 and ending at 1.
We can replace the original element in the list if we change the ending index in the slicing. It will replace the original elements with the new elements in the specified index range. Let's understand this with the help of an example.
Output:
In the above code, we can see that code block numlist[1:2] adds the new elements in the original list starting from index position one, but it replaces the elements at the 2nd index position in the num_list.
Conclusion
- Elements can be added to the list using built-in functions like append(), insert().
- append() adds the elements at the end of the list, while insert() adds the element at the specified position.
- extend() function and + operator is used to concatenate elements from one list/tuple into the other.
- Slicing can be used to add a sequence of elements to the existing list.