Insert into list Python

In this article we will discuss how to insert an element in list at specific position.

Inserting an element in list at specific index using list.insert[]

In python list provides a member function insert[] i.e.

list.insert[position, element]
It accepts a position and an element and inserts the element at given position in the list.

Lets see an example,

Suppose we have a list of strings i.e.

# List of string list1 = ['Hi' , 'hello', 'at', 'this', 'there', 'from']
Now let insert why at 3rd position in the list i.e.
# Add an element at 3rd position in the list list1.insert[3, 'why']
Index will start from 0 in list. So, element will be inserted at 3rd position i.e. after 0,1 & 2.
Advertisements

So, list contents will be now,

['Hi', 'hello', 'at', 'why', 'this', 'there', 'from']

Inserts an element at beginning of list

To insert the element at the front of above list, call insert[] function i.e.

# Add an element at the front of list list1.insert[0, 'city']
So, list contents will be now,
['city', 'Hi', 'hello', 'at', 'why', 'this', 'there', 'from']

Insert all elements of another list at specific index in given list

Suppose we have two lists i.e.

list1 = ['city', 'Hi', 'hello', 'at', 'why', 'this', 'there', 'from'] list2 = [3,5,7,1]
Now Insert all the elements of list2 at 3rd position in list1

Method 1:

Iterate over list2 in reverse and keep on inserting element at 3rd index in list1 using list.insert[] i.e.


# Insert all the elements in list2 to list1 between 3 to 4 th element for elem in reversed[list2] : list1.insert[3, elem]
Method 2:

Splice list1 from 0 to 2 and merge all elements of list2 in it. Then merge all the remaining elements of list from 3 to end i.e.

# Insert all the elements in list2 to list1 between 3 to 4 th element list1 = list1[:3] + list2 + list1[3:]
In both cases lists content will be now,
['city', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'why', 'this', 'there', 'from']
Complete example is as follows,
''' Inserting all elements of list1 at specific index in other list ''' def main[]: # List of string list1 = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # Print the List print[list1] # Add an element at 3rd position in the list list1.insert[3, 'why'] # Print the List print[list1] # Add an element at the front of list list1.insert[0, 'city'] # Print the List print[list1] list2 = [3,5,7,1] # Insert all the elements in list2 to list1 between 3 to 4 th element for elem in reversed[list2] : list1.insert[3, elem] # Print the List print[list1] # List of string list1 = ['Hi' , 'hello', 'at', 'this', 'there', 'from'] # Insert all the elements in list2 to list1 between 3 to 4 th element list1 = list1[:3] + list2 + list1[3:] # Print the List print[list1] if __name__ == '__main__': main[]
Output:
['Hi', 'hello', 'at', 'this', 'there', 'from'] ['Hi', 'hello', 'at', 'why', 'this', 'there', 'from'] ['city', 'Hi', 'hello', 'at', 'why', 'this', 'there', 'from'] ['city', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'why', 'this', 'there', 'from'] ['Hi', 'hello', 'at', 3, 5, 7, 1, 'this', 'there', 'from']
Advertisements

Video liên quan

Chủ Đề