How to Add an Element to a List in Python

How to Add an Element to a List in Python | To add an element to a list in python there are many ways and also python provides various methods to do so. Also see:- How to Convert List to String in Python

We will see these below Python program examples:-

  1. Using append()
  2. Using insert()
  3. Using extend()
  4. Append to empty list python
  5. Add element to array python numpy
  6. Add elements in list in python using for loop
  7. Python list add element to front
  8. Python list add element at index
  9. Python list add element to end
  10. Python append multiple items to list

How to Add an Element to a List in Python

Now let us see some of the built-in methods to add an element in a list in python.

Python List Add Element using append()

First, we will go through the append() method, we add elements to the list through the append() method.

l = list(range(3))
print(l)

l.append(10)
print(l)

l.append('abc')
print(l)

Output:

[0, 1, 2]
[0, 1, 2, 10]
[0, 1, 2, 10, ‘abc’]

Python List Add Element using insert()

The insert() method adds the element to the list at the specified index, it takes two number parameters first indicates the index, and second, indicates the element to be added.

l = list(range(4))
print(l)

l.insert(1, 10)
print(l)

l.insert(-1, 20)
print(l)

Output:

[0, 1, 2, 3]
[0, 10, 1, 2, 3]
[0, 10, 1, 2, 20, 3]

Python List Add Element using extend()

The extend() method adds the element to the end of the list that is it extends the list. If you add any string element if splits the string into character and add it to the list.

l = list(range(4))
print(l)

l.extend([10, 11, 12])
print(l)

l.extend((-1, -2, -3))
print(l)

l.extend(('abc'))
print(l)

Output:

[0, 1, 2, 3]
[0, 1, 2, 3, 10, 11, 12]
[0, 1, 2, 3, 10, 11, 12, -1, -2, -3]
[0, 1, 2, 3, 10, 11, 12, -1, -2, -3, ‘a’, ‘b’, ‘c’]

Append to Empty List in Python

We can add an element to the empty, to do this first create an empty element by using the append() method.

l = []
print(l)

l.append([10, 11, 12])
print(l)

Output:

[ ]
[[10, 11, 12]]

Add Element to Array Python using Numpy

NumPy is a module in python which provides various operations and is used for working with arrays, expanded as Numerical Python.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
new_arr = np.append(arr, 11)

print('Original Array:', arr)
print('New Array:', new_arr)

Output:

Original Array: [1 2 3 4 5]
New Array: [ 1 2 3 4 5 11]

Add Elements to a List in Python using For Loop

By using a range in for loop we add list elements, we use for loop to iterate over all the elements. In the range method, we have mentioned 3 so that the list contains only 3 elements.

list=[]
for i in range(3):    
   list.append(i)
print(list)

Output:

[0, 1, 2]

Python List Add Element to Front

To add an element to the ist in starting we can use [ ] and + so that it adds the element at the starting of the list. Or else we can use insert() also which adds the list element in the specified index.

list = [1,2]
print(list)
l = [3]+ list
print(l)

Output:

[1, 2]
[3, 1, 2]

Python List Add Element at Index

We can do this by using the insert() method which adds the element into the list at the specified index. The below list initially, contains only string elements that are a, b, and c, and later we insert 1 to the second index.

list = ['a','b','c']
print(list)
list.insert(2, 1)
print(list)

Output:

[‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, 1, ‘c’]

Python List Add Element to End

To add the element to the list at the end, use extend() method, this method by default adds the element to the end of the list.

list = ['a','b','c']
print(list)
list.extend([2])
print(list)

Output:

[‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, ‘c’, 2]

Python Append Multiple Items to List

To append multiple items we just use the ’+’ operator. Observe the below code we have to create more than one list and use + operator to attach those lists to one.

list = ['a','b','c']
list1 = [1,2,3]
list2 = ['A','B','C']
list3 = ['abc']

print(list)
print(list1)
print(list2)
print(list3)

res = list+ list1 + list2 +list3
print(res)

Output:

[‘a’, ‘b’, ‘c’]
[1, 2, 3]
[‘A’, ‘B’, ‘C’]
[‘abc’]
[‘a’, ‘b’, ‘c’, 1, 2, 3, ‘A’, ‘B’, ‘C’, ‘abc’]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.