You are currently viewing Add Element to List by Index in Python

How to add an element in the list by index or position in Python? You can add an element to the list at a specific index using the insert(). By using this you can add an element at the first index, last index, or at any specific index. This method takes the integer as a position where you wanted to add and the element to be added as arguments.

Advertisements

In this article, I will explain how to add an element to a list at a specific position using insert(). To add an element to the end you can also use the append().

1. Quick Examples to Add Element to List by Index

Following are quick examples of how to add elements to a list at a specific position or index.


# Quick Examples

# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add element at first position
mylist1.insert(0,"java") 

# Add element at last position
mylist1.insert(len(mylist1),"python") 

# Add list at fourth position
mylist1.insert(3,["java",".net"])

# Add tuple as element to list
mylist1.insert(3,("java",".net"))

# Add set
mylist1.insert(3,{"java",".net"})

# Add dictionary of keys
mylist1.insert(3,{"s1":"java","s2":".net"})

# Add dictionary of values
mylist1.insert(3,{"s1":"java","s2":".net"}.values())

2. Add Element to List at a Specific Index in Python

We are often required to add an element to a list in python, you can easily achieve this by using the insert() method. It will take two parameters, the first parameter is the index position and the second parameter is the element/iterable to be added. For the second parameter, you can use either element of regular data type or iterable objects like a list, set, tuple, or dictionary. It will consider the entire iterable as one element when adding to the python list.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add element at last
mylist1.insert(4,"python") 
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['hello', 'welcome', 'to', 'sparkby', 'python', 'examples']

Here, we added the element python to the list in the 5th position (4th index). Note that index in python starts from 0. hence, the element added at the 4th index is actually added to the list as a 5th element.

3. Add Element to List at Last Index Position

If you want to add an element at the last index position of the python list, you can use the len() to find the total number of elements in the list and use this as an index to the along with the element you wanted to add.

Let’s create a list of strings and add python at the last position on the list. You can also use the append() instead.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add element at last
mylist1.insert(len(mylist1),"python") 
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['hello', 'welcome', 'to', 'sparkby', 'examples', 'python']

You can see that python is added at the last position in the list.

4. Add Element to List at First Index Position

Let’s add an element at the first position, to do so you can directly specify the index as 0, as it refers to the starting position. Let’s create a list of strings and add java at the first position on the list.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add element at first
mylist1.insert(0,"java") 
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['java', 'hello', 'welcome', 'to', 'sparkby', 'examples']

You can see that “java” is added at the first position in the list.

5. Add Tuple as Element to List

Let’s add a tuple of elements at the third position in the existing list.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add tuple
mylist1.insert(3,("java",".net"))
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['hello', 'welcome', 'to', ('java', '.net'), 'sparkby', 'examples']

We are considering a tuple that holds 2 strings. Now we added this tuple at the third index position. The list will consider this entire tuple as a single element.

6. Add List as an Element

Let’s add a list of elements at the third position in the existing list. For more similar examples, refer to Different ways to append multiple items to the list.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add list
mylist1.insert(3,["java",".net"])
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['hello', 'welcome', 'to', ['java', '.net'], 'sparkby', 'examples']

We are considering a list that holds 2 strings. Now we added this list at the third index position in the existing list.

7. Add Set as an Element to the List

Let’s add a set of elements at the fifth position in the existing list.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add set to the list
mylist1.insert(5,{"java",".net"})
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['hello', 'welcome', 'to', 'sparkby', 'examples', {'java', '.net'}]

8. Add Dictionary as an Element

Let’s add Dictionary items at the second position in the existing list.


# Consider the list of strings
mylist1=['hello','welcome','to','sparkby','examples']
print("Actual List: ",mylist1)

# Add dictionary
mylist1.insert(2,{"s1":"java","s2":".net"})
print(mylist1) 

# Output:
# Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
# ['hello', 'welcome', {'s1': 'java', 's2': '.net'}, 'to', 'sparkby', 'examples']

In the above code, we added the dictionary that holds two items at the second index position in the list.

9. Conclusion

This article shows different ways to add single or multiple elements at a specific index on the python list. For better understanding, we explained adding a single element to the list, adding iterable to the list. You can also add nested data structures like the list of tuples/ list of lists etc to the list.

References