You are currently viewing Add Elements to a List in Python

How to add elements to the list in Python? You can add an element or several elements to a list by using many ways, for example, using the + operator, append(), insert(), extend(), and list comprehension. In this article, I will explain adding elements to a list in python by using all these methods with examples.

Advertisements

Below are methods to add elements to a list in python:

  • Use append() to add an element (string, number, iterable, etc.) to the end of a list.
  • Use insert() to add an element at a specific index in a list.
  • Use extend() add iterable (e.g. list, tuple) to the end of the list.
  • Use + operator to concatenate two lists together.
  • Use list comprehension to add elements to a list based on a certain condition or operation.

1. Quick Examples of Adding Elements to a List

If you are in a hurry, below are some quick examples of adding elements to a list in python.


# Below are the quick examples

# Example 1: Add an element to the list
# Using append() function
technology = ['Spark','Python','Pyspark']
technology.append('Java')

# Example 2: Add list to the list 
# Using append() function
technology1 = ['Hyperion', 'Pandas', 'Pyspark']
technology.append(technology1)

# Example 3: Add element at specific position
# Using insert() function
technology.insert(0, 'Hadoop')

# Example 4: Add list by extending elements
# Using extend() function 
technology1 = ['Pandas', 'Pyspark']
technology.extend(technology1)

# Example 5: Add tuple by extending elements
technology1 = ('Spark', 'Python')
technology.extend(technology1)

# Example 6: Use the + operator
technology = ['Spark', 'Python']
technology1 = ['Pandas', 'Pyspark']
technology += technology1

# Example 7: Use + operator
numbers1 = [2, 4, 6]
numbers2 = [1, 3, 5]
numbers = numbers1 + numbers2

# Example 9: Using list comprehension
numbers = [2, 4, 6]
numbers = numbers + [x for x in range(7, 9)]

2. Add Elements to List Using append()

You can use the list append() function to add an element or list of elements in Python. For example, if you have a list called technology and you want to add the element, you would use the following code.


# Add an element to the list
technology = ['Spark','Python','Pyspark']
print("Actual List",technology)

# Add 'Java' to the list
technology.append('Java')
print("Updated List: ",technology)

This example yields the below output.

python add element list

Similarly, you can also use this function to add a list of elements to a list in Python. For example, create list technology1 and add to list technology. Now technology contains the elements of the original list and the list you added. When you add a list, it adds list as an element resulting nested list.


# Create two lists
technology = ['Java', 'Spark', 'Python']
technology1 = ['Hyperion', 'Pandas', 'Pyspark']
print("Original List 1: ",technology)
print("Original List 2: ",technology1)

# Add elements to the list using append() function
technology.append(technology1)
print("Updated List 1: ",technology)

This example yields the below output. Notice that the list is added as a single element resulting nested list.

python add elements list

3. Add Elements to List Using insert()

You can use the list.insert() to add an elements at a specific index in the python list. For example, you can insert 'Hadoop' at the first position on the list. For more examples refer to add element to list by index.


# Add elements to list using insert() function
technology = ['Spark','Python','Pyspark']

# Insert first position
technology.insert(0, 'Hadoop')
print(technology)

# Output:
# ['Hadoop', 'Spark', 'Python', 'Pyspark']

# Insert second position
technology = ['Spark','Python','Pyspark']
technology.insert(1, 'Hyperion')
print(technology)

# Output:
# ['Spark', 'Hyperion', 'Python', 'Pyspark']

4. Add Elements to List Using extend() Function

The list.extend() adds the elements to the existing list at the end. When you add an iterator by using this method, it actually extends the elements as a separate element and adds them to the list. Note that this modifies the existing list with the new elements and the elements are added at the end of the original list. For more examples refer to append multiple elements to the list.


# Use extend() function 
technology = ['Spark', 'Python']
technology1 = ['Pandas', 'Pyspark']

# Extend list to list
technology.extend(technology1)
print(technology)

# Output:
# ['Spark', 'Python', 'Pandas', 'Pyspark']

Use add elements from the tuple to insert into the list. Here, my list had 2 elements to start with, and added a tuple of 3 elements, after adding a tuple, there are 5 elements in the list.


# Consider the list of strings
technology = ['Spark', 'Python']
print("Actual List: ",technology)

# Extend tuple to list
technology.extend(('Java','Pandas','Pyspark'))
print("Final List: ",technology)

# Output:
# Actual List:  ['Spark', 'Python']
# Final List:  ['Spark', 'Python', 'Java', 'Pandas', 'Pyspark']

5. Add Elements to List Using + Operator

You can also use the "+" operator to concatenate two lists together. For example,


# Use the += operator
technology = ['Spark', 'Python']
technology1 = ['Pandas', 'Pyspark']
technology += technology1
print(technology)

# Output:
# ['Spark', 'Python', 'Pandas', 'Pyspark']

# Use + operator
numbers1 = [2, 4, 6]
numbers2 = [1, 3, 5]
numbers = numbers1 + numbers2
print(numbers)

# Output:
# [2, 4, 6, 1, 3, 5]

6. Using List Comprehension

You can use list comprehension to add an element to a list based on a certain condition or operation. For example,


# Using list comprehension
numbers = [2, 4, 6]
numbers = numbers + [x for x in range(7, 9)]
print(numbers)

# Output:
# [2, 4, 6, 7, 8]

7. Using * Operator

Use * operator to repeat the elements of a list and add back to list.


# Using * operator
number = [1,2,3]
number = number * 2
print(number)

# Output:
# [1, 2, 3, 1, 2, 3]

Conclusion

In this article, I have explained how to add elements to a list in python by using append(), insert(), extend(), list comprehension, and + operator with examples.

Happy Learning !!