This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
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.
The post How to Append List Elements in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-25T11:03:10+00:00) How to Append List Elements in Python. Retrieved from https://www.scien.cx/2021/02/25/how-to-append-list-elements-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.