© 2004-21

Managing a 1D List

Ths code snippet below shows you how find an item an item in a list and count the number of times it occurs.
NOTEthe in operator returns the index number of the first element where the item is found

# Author A Knifton
# Date
# Purpose set up and manage a list
# set up the initial contents of the list
fruitList = ["orange","apple", "pear","mango", "grapefruit", "lemon", "kiwi", "plum","peach", "apple"]

# print the length of the list using the len() function
itemsInList = len(fruitList)
print("In the list {}\nThere are {} items\n ".format(fruitList,itemsInList))

#How many time does an item occur in the list
findFruit = input("Which fruit do you want to find?")
itemCount = fruitList.count(findFruit)
print("The fruit {} occurs {} the fruitList ".format(findFruit,itemCount))

#what is the index number of the item?
if findFruit in fruitList:
    print("{} is in fruitList at index number: {}".format(findFruit,fruitList.index(findFruit)))