In this post, We will learn how to Append multiple elements List in Python. We will cover Append single or multiple elements List in Python. by using the append() or extend(), insert() method.
How to Append multiple elements List in Python
- append() – add an element or list object to the end of the existing list.
- extend()– merge list
- insert()– add an element at specific index.
- slice – Add a list or tuple at the specified index
- Insert a list at the index of the existing list
- Slicing to add Mutiple element as a list at index
- Insert Mutiple tuple elements at a specific Index
- How to create add update delete List Python
- 5 Ways to get unique items from a list in python
- 4 Ways to take input from the user in the Python list.
- 4 Ways to Find index-value all items a Python list
1. How to Append multiple elements List in Python
The list. append() method is used to add an element or a list at the end of at end of the existing list.
Syntax
list.append(element);
Parameter:
- element: the element we want to add at end of the list
Program Example
In this example, we have added a single element 12 to the list by using using the list. append() method, it will end of the list.
mylist = ['C#','lang','Go','Data']
mylist.append(12)
print('list after append element = \n',mylist)
Output
list after append element =
['C#', 'lang', 'Go', 'Data', 12]
2. How to Append a list to an existing list using list.append()
The list. append() method can be used to add a list to the end of the existing list. The append() method append the whole list object at end of an existing list as an inner list, not element by element separated by a comma(‘).
Program Example
mylist = ['C#','lang','Go','Data']
lstnum = ['16','17','35','68']
#appending lstnum object
mylist.append(lstnum)
print('list after append element = \n',mylist)
Output
list after append element =
['C#', 'lang', 'Go', 'Data', ['16', '17', '35', '68']]
3. How to Append multiple elements List in Python using List. extend()
The extend() method is used to combine two lists and add all elements of a list to the end of another list separated by a comma(,).
Program Example
mylist = ['C#','lang','Go','Data']
lstnum = ['16','17','35','68']
#merging lstnum object in mylist
mylist.extend(lstnum)
print('adding all element of list = \n',mylist)
Output
From the above output, you can see we could achieve the desired results and add two lists to form a single list.
adding all element of list =
['C#', 'lang', 'Go', 'Data', '16', '17', '35', '68']
4. Insert an item at a specific index in List using insert()
Sometimes we have the need to add an element to a specific index in the existing list. To get this done we use the insert() method of the list.
Syntax
list.insert(index,element)
Parameters
- Index: The index of the list at which element to be inserted element.
- Element: The second parameter is an element to insert. The index start is
0
. For negative values,-1
means one before the end or you can say in reverse.
Program Example
mylist = ['C#','lang','Go','Data']
#insert element 6 at index 1 using insert() method
mylist.insert(1,6)
mylist.insert(-1,5)
print('result list adding element at specific Index = \n',mylist)
Output
result list adding element at specific Index =
['C#', 6, 'lang', 'Go', 5, 'Data']
5. How to Append multiple elements List in Python at a specific index
The list. insert() method can be used to add a list of multiple elements at a specific index. We have to pass the index and list that we want to insert at a specific index. In the below example we have inserted a list at index 2 existing list. Let us understand the behavior of the list.insert() method when we insert at a specific index.
mylist = ['C#','lang','Go','Data']
lstnum = ['16','17','35','68']
#inserting lstnum object in mylist
mylist.insert(2,lstnum)
print('list after insert another list at specific Index = \n',mylist)
Output
From the above output, you can see that the whole list object is inserted at index 2.
list after insert another list at specific Index =
['C#', 'lang', ['16', '17', '35', '68'], 'Go', 'Data']
6. Slicing to append a list of mutiple elements at a specific Index
To add a list of multiple elements at a specified index we can use list slicing. In the below example we have inserted multiple elements starting from index 0 and at the end at index 1. so the element at index is 1 overridden.
Program Example
mylist = ['C#','lang','Go','Data']
lstnum = ['16','17','35','68']
#inserting lstnum object in mylist
mylist[0:1] = lstnum
print('add list a specific index = \n',mylist)
Output
add list a specific index =
['16', '17', '35', '68', 'lang', 'Go', 'Data']
7. Slicing to Insert Mutiple tuple elements at a specific index
you will see that all existing elements of the list got overwritten by the new values which we assigned from Index 0 to index 3. 4th index is not included in slicing.
mylist = ['C#','lang','Go','Data']
#inserting tuple at speific IndexError
mylist[0:4] = ('16','17','35','68')
print('add tuple at specific index = \n',mylist)
Output
add tuple at a specific index =
['16', '17', '35', '68']
Summary
in this article, we have learned How to Append multiple elements List in Python with examples by using the different list methods