Add element to list Python | Example code

  • by

Use list append(), extend(), and insert() method to add elements to a list in Python. Simple use of the + operator also adds an element into a list or uses slices to insert items at specific positions.

  • append(): Add an element to the end
  • extend(): Add a list at the end of the other list
  • insert(): Insert an item at the specified index
  • + operator

Example Add an element to list Python

Simple example code.

Add an item to the end: append()

list1 = [1, 2, 3]



Add element to list Python

Combine two lists: extend()

list1 = [1, 2, 3]


Output: [1, 2, 3, 4]

Insert an item at specified index: insert()

The first parameter is the index value and the element to be inserted for the second parameter.

Inserting element at zero position.

list1 = [1, 2, 3]
list1.insert(0, 4)


Output: [4, 1, 2, 3]

+ operator

list1 = [1, 2, 3]

print(list1 + [4])

Output: [1, 2, 3, 4]

Do comment if you have any doubts or suggestions on this Python List topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.