You can loop through the list items by using a for
loop:
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
You will learn more about for
loops in our Python For Loops Chapter.
To determine if a specified item is present in a list use the in
keyword:
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
To determine how many items a list has, use the len()
method:
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
To add an item to the end of the list, use the append() method:
Using the append()
method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)