3 ways to add items to a Python list without using append():
We can add items to a Python list without using the append()
method. In this post, we will learn three different ways to add items to a list.
Method 1: By using the ’+’ operator:
This is the easiest way to add items to a list in Python. The plus operator or +
can be used to add items to a list. We can add a list with a single item or multiple items to another list. Let’s take a look at the below program:
given_list = []
given_list += [1]
given_list += [2]
given_list += [3]
given_list += [4]
print(given_list)
In this example, we are adding different lists with a single item to the empty list given_list
. It will print:
[1, 2, 3, 4]
Similarly, we can also append a list with multiple items to another list:
given_list = []
given_list += [1]
given_list += [2]
given_list += [3]
given_list += [4]
given_list += [5, 6, 7, 8]
print(given_list)
It will add the list items to the empty list given_list
. It will print the below output:
[1, 2, 3, 4, 5, 6, 7, 8]
Download this example on Github
Method 2: By using the extend() method:
The extend()
method can append a list to the end of another list. It takes the list as the parameter. The extend()
method modifies the original caller list and it returns None. Let’s take a look at the example below:
given_list = [1, 2, 3, 4, 5]
given_list.extend([6, 7, 8])
print(given_list)
It will print the below output:
[1, 2, 3, 4, 5, 6, 7, 8]
We can call it multiple times on the same list to append multiple items to a list.
given_list = [1, 2, 3, 4, 5]
given_list.extend([6, 7, 8])
given_list.extend([9, 10])
print(given_list)
It will print:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Download this example on Github
Method 3: By using list slicing:
List slicing is another way to add one or more items to a list in Python. Python list slicing is used to get a sub-list from a list. The syntax of slicing is:
list[start:end:step]
It will return the sub-list from the index start
to end
with a step size step
. These values are optional. The default value of start
is 0, end
is the length of the list and step
is 1.
For example,
given_list = [1, 2, 3, 4, 5]
print(given_list[:2])
print(given_list[2:])
Here, the first statement given_list[:2]
will return the items from index 0 to 1 and the second statement given_list[2:]
will return the items from index 2 to the end of the list. It will print the following output:
[1, 2]
[3, 4, 5]
We can use list slicing to add items to the start or end of a list. For example, the below program adds the items to the start of the given list:
given_list = [1, 2, 3, 4, 5]
given_list[:0] = [6, 7, 8]
print(given_list)
It will print the below output:
[6, 7, 8, 1, 2, 3, 4, 5]
The list slicing given_list[:0]
uses the end
value 0 and the default start
value i.e. 0. So, it adds the items to the start of the list.
Similarly, the below script will add items to the end of the list:
given_list = [1, 2, 3, 4, 5]
size = len(given_list)
given_list[size:] = [6, 7, 8]
print(given_list)
It will print:
[1, 2, 3, 4, 5, 6, 7, 8]
We are using the length of the list as the start
value for the slicing. So, it adds the items to the end of the list.
Download this example on Github
You might also like:
- Python Set intersection_update() Method
- How to find the md5 hash of a file in python
- How to find the system battery information using Python
- How to find all running processes using python
- How to find the relative path of a file in python
- Python program to check if the characters in a string are sequential