How to Add Elements to a List in Python?

How to Add Elements to a List in Python?

This tutorial will explore various methods for appending elements to a list in Python.

There are four methods to add elements to a List in Python.

  1. append(): append the element to the end of the list.
  2. insert(): inserts the element before the given index.
  3. extend(): extends the list by appending elements from the iterable.
  4. List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.

1. append()

This function appends a single element to the end of the list.


	fruit_list = ["Apple", "Banana"]
	print(f'Current Fruits List {fruit_list}')
	new_fruit = input("Please enter a fruit name:n")
	fruit_list.append(new_fruit)
	print(f'Updated Fruits List {fruit_list}')

Output

Current Fruits List [‘Apple’, ‘Banana’]
Please enter a fruit name:
Orange
Updated Fruits List [‘Apple’, ‘Banana’, ‘Orange’

In this example, “Orange” was appended to the end of the list.

2. insert()

This function inserts an element at the specified index within the list.


	num_list = [1, 2, 3, 4, 5]
	print(f'Current Numbers List {num_list}')
	num = int(input("Please enter a number to add to list:n"))
	index = int(input(f'Please enter the index between 0 and {len(num_list) - 1} to add the number:n'))
	num_list.insert(index, num)
	print(f'Updated Numbers List {num_list}')

Output

Current Numbers List [1, 2, 3, 4, 5]
Please enter a number to add to list:
20
Please enter the index between 0 and 4 to add the number:
2
Updated Numbers List [1, 2, 20, 3, 4, 5]

In this instance, the number 20 was added at index 2. This resulted in the insertion of 20 into the list at the specified index.

3. extend()

This function appends elements from an iterable to the list.


	extend_list = []
	extend_list.extend([1, 2])  # extending list elements
	print(extend_list)
	extend_list.extend((3, 4))  # extending tuple elements
	print(extend_list)
	extend_list.extend("ABC")  # extending string elements
	print(extend_list)

Output

[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, ‘A’, ‘B’, ‘C’]

In this example, a list [1, 2] was added, followed by the inclusion of a tuple (3, 4), and finally, the addition of a string “ABC”.

4. List Concatenation

To concatenate multiple lists, you can utilize the + operator. This action creates a fresh list, leaving the original lists unaffected.


	evens = [2, 4, 6]
	odds = [1, 3, 5]
	nums = odds + evens
	print(nums)

Output

[1, 3, 5, 2, 4, 6]
In this scenario, the list of even numbers was appended to the end of the list of odd numbers. The resulting list will encompass elements in the order they appear, left to right.

Conclusion

Python offers various techniques for including elements in a list. Elements can be appended to the list’s end or inserted at a specific index. Lists can also be combined by adding one list to another. To concatenate multiple lists, employ the overloaded + operator.

Thanks for learning with the Accuweb.Cloud. Check out our offerings for compute, storage, databases.
Deploy Your Python Application Quickly!