python

How to Append List Elements in Python


In this article, you will learn how to append list elements in Python.

Let’s say you have 2 lists of food.

# A list named 'food' with value ["Pizza", "Burger"]
food = ["Pizza", "Burger"]

# A list named 'moreFood' with value ["Waffle", "Fried Chicken"]
moreFood = ["Waffle", "Fried Chicken"]

Append List Elements in Python

In order to append list elements, you can use the list.extend() keyword.

# A list named 'food' with value ["Pizza", "Burger"]
food = ["Pizza", "Burger"]

# A list named 'moreFood' with value ["Waffle", "Fried Chicken"]
moreFood = ["Waffle", "Fried Chicken"]

# Append list named 'moreFood' at the end of list named 'food'
food.extend(moreFood)

# Display list named 'food'
print(food)
# ['Pizza', 'Burger', 'Waffle', 'Fried Chicken']

Note: The list.extend() keyword functions by appending the elements of supplied iterable at the end of list.


Share on social media

//