Loading lesson path
Concept visual
To add an item to the end of the list, use the append() method:
method to append an item: thislist = ["apple", "banana", "cherry"] thislist.append("orange")
print(thislist)To insert a list item at a specified index, use the insert() method.
method inserts an item at the specified index:
Insert an item as the second position: thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange")
print(thislist)As a result of the examples above, the lists will now contain 4 items.
To append elements from another list to the current list, use the extend() method.
thislist = ["apple", "banana", "cherry"] tropical = ["mango", "pineapple", "papaya"] thislist.extend(tropical)
print(thislist)The elements will be added to the end of the list.
method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.).
Add elements of a tuple to a list: thislist = ["apple", "banana", "cherry"]
Formula
thistuple = ("kiwi", "orange")thislist.extend(thistuple)
print(thislist)