Saturday, November 16, 2024
Google search engine
HomeLanguagesPython Add Element at specific Index in List

Python Add Element at specific Index in List

Python program to insert or add an element at a specific position or index into a given list; Through this tutorial, you will learn how to add an element at a specific index position in python.

Python Add Element at Specified Index in List

  • 1: Inserting an element in list at specific index using list.insert()
  • 2: Inserts an element at beginning of list
  • 3: Insert all elements of another list at specific index in given list

1: Inserting an element in list at specific index using list.insert()

For inserting an element in list at a particular/specific index/position, python provides function name insert().

Syntax of index() function:

 list.insert(position, element) 

It accepts two parameters, first is the position of the element and second one is element to inserts the element at a given position in the list.

Here, we will take several examples of index() function, Let’s see an examples,

Assume we have simple list of strings i.e.

 List of string list1 = ['Hi' ,  'hello', 'at', 'this', 'there', 'from'] 

Now let insert ‘world’ at 3rd position in the list i.e.

 # Add an element at 3rd position in the 
listlist1.insert(3, 'world') 

Index will always start from 0 in list. So, element will be inserted at 3rd position i.e. after 0,1 & 2.

So, list contents will be now,

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

2: 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 
listlist1.insert(0, 'my') 

So, list contents will be now,

 ['my', 'Hi', 'hello', 'at', 'world', 'this', 'there', 'from'] 

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

Suppose we have two lists i.e.

 list1 = ['my', 'Hi', 'hello', 'at', 'world', '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 
 elementlist1 = list1[:3] + list2 + list1[3:] 

In both cases lists content will be now,

 ['my', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'world', 'this', 'there', 'from'] 

The given below python program contain all the above examples:

'''
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, 'world')
# Print the List
print(list1)
# Add an element at the front of list
list1.insert(0, 'my')
# 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', 'world', 'this', 'there', 'from']
 ['my', 'Hi', 'hello', 'at', 'world', 'this', 'there', 'from']
 ['my', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'world', 'this', 'there', 'from']
 ['Hi', 'hello', 'at', 3, 5, 7, 1, 'this', 'there', 'from']

Recommended Python List Programs

  1. Python Print List Elements in Different Way
  2. How to Input List From User in Python
  3. Python Add and Remove Elements From List
  4. Python Program to Sort List in Ascending and Descending Order
  5. Python to Find the Differences of Two Lists
  6. Python to Find Minimum and Maximum Elements of List
  7. Python Programs to Split Even and Odd Numbers in Separate List
  8. Python Program to Create Two Lists with First Half and Second Half Elements of Given List
  9. Python Program to Swap Two Elements in a List
  10. Python Program to Reverse List
  11. How To Select Random Item From A List In Python
RELATED ARTICLES

Most Popular

Recent Comments