Python List Append

Source URL: https://www.journaldev.com/33182/python-add-to-list

Language: python

Created on: 2020-12-01 19:15:35

# Python list mutation, adding elements
history = ["when"]

# adds item to the end of a list
history.append("how")
# ["when", "how"]

# combine lists
history.extend( ["what", "why"] ) # works with tuples too
# or
history = history + ["what", "why"]
# ["when", "how", "what", "why"]

# insert at target position
history.insert(3, "where")
# ["when", "how, "what", "where", "why"]
#
How to add elements to a List in Python. Python add to List methods - append(), insert(), extend(), list concatenation. Adding element to given index example.
content_copy