Post

Python List append()

The append() method will add an element at the end of the list.

Python List append()

The syntax of the append() method is:

1
2
list.append(element)

append() parameters

The append() method only takes one argument as a parameter:

  • element - An element (integer, string, dictionary, or another list) to be added at the end of the list.

Let’s check some examples of the list append() method.

Example 1: How to add an element to a list?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
my_cars = ["AUDI","BMW","FORD"]

# Old list is

print(my_cars)

# adding TATA to the cars list

my_cars.append("TATA")

#updating list is

print(my_cars)

The Output will be as follow

1
2
3
['AUDI', 'BMW', 'FORD']
['AUDI', 'BMW', 'FORD', 'TATA']

Example 2: How to add a dictionary into a python list?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my_cars = ["AUDI","BMW","FORD"]

new_car = {"TATA": 31}
# Old list is

print(my_cars)

# adding TATA to the cars list

my_cars.append(new_car)

#updating list is

print(my_cars)

The output will be as follows.

1
2
3
['AUDI', 'BMW', 'FORD']
['AUDI', 'BMW', 'FORD', {'TATA': 31}]

Rules of list append()

  • The append() method does not return any value.
  • The append() method can insert any data types to list.(Integer, float, set, list , dictionary, etc)
This post is licensed under CC BY 4.0 by the author.