How to do Append multiple elements List in Python

Python List

In this post, how to Append single or multiple elements in Python List. We will learn different ways of How to do Append multiple elements List in Python. add list elements separated by a comma at end of the existing list using the append() or extend(), insert() method.

How to do 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 list or tuple at specified index
  • Insert a list at index of existing list
  • Slicing to add Mutiple element as list at index
  • Insert Mutiple tuple elements at specific Index

1. List.append() to append single elements


The list. append() method is used to add an element at end of the existing list.

Syntax

list.append(element);

Parameter:

  • element : the element we want to add at end of 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. Append() to Append list to existing list


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 the list, not a single 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. List.extend() to Append Mutiple elements


The extend() method is used to combine two lists, add all elements of a list to the end of another list separated by 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() to insert an item at specific index


Sometimes we have the need to add an element at 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: is the index at which element to insert 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. Insert a list at index of existing list


Same as append() method insert() also allow us to add an element at a time.

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 add Mutiple element as list at index


To add a list of multiple elements at specified index we can use list slicing.In the below example we have inserted multiple elements start from index 0 and at end at index 1. so elemnt at index is 1 overrided.

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. Insert Mutiple tuple elements at 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 understood how to Append single or multiple elements in Python List.