How to include items to a list in Python
In this section, we will provide an introduction.
In this guide, we will explore various methods of including items to a Python list.
Python offers four different techniques for adding elements to a List.
-
- append(): Adds the element to the end of the list.
-
- insert(): Places the element before the specified index.
-
- extend(): Expands the list by adding elements from an iterable.
- List Concatenation: The + operator can be used to combine multiple lists and generate a new list.
Info
Requirements
To finish this tutorial, you will require:
Familiarity with installing Python 3. And familiarity with coding in Python. How to Code in Python 3 series or using VS Code for Python.
I have personally verified that this tutorial works well with Python 3.9.6.
add to
This operation appends one 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}')
The result:
Current Fruits List ['Apple', 'Banana']
Please enter a fruit name:
Orange
Updated Fruits List ['Apple', 'Banana', 'Orange']
The list was extended by including Orange at the end.
add in
This operation inserts an element into the list at the specified index.
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}')
Result:
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 included at position 2 within the list.
expand()
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)
One possible paraphrase is: “The result:”
[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, 'A', 'B', 'C']
In this illustration, a series of elements [1, 2] was included. Following that, a tuple (3, 4) was appended. Lastly, a string ABC was appended as well.
Combining lists
To merge several lists, you can utilize the + operator, creating a fresh list while leaving the original lists unaffected.
evens = [2, 4, 6]
odds = [1, 3, 5]
nums = odds + evens
print(nums) # [1, 3, 5, 2, 4, 6]
The given instance appended the list of even numbers to the list of odd numbers. The resulting list will have elements in the order they appear in the original lists. This operation resembles Python’s string concatenation.
In summary
Python has various methods to include elements into a list. We have the option to append an element at the end of the list or insert an element at a specific index. Additionally, we can combine one list with another. When you need to merge multiple lists, you can utilize the + operator.
Citations:
Sources:
Bibliography:
- Python List
- Python.org Docs
more tutorials
Set in Python(Opens in a new browser tab)
How can I include new entries in a Python dictionary?(Opens in a new browser tab)
jQuery parent() and children() tree traversal functions(Opens in a new browser tab)
Python Substring refers to extracting a smaller portion of a string.(Opens in a new browser tab)