External Exam Download Resources Web Applications Games Recycle Bin

Lists

simple list.py

foods = ["Sushi", "Tacos", "Pizza", "Vegetable Soup"]
print(foods[2]) #Pizza

import random 
cards = ["TEN", "JACK", "QUEEN", "KING", "ACE"]
card = random.choice(cards)
print(card)

population = [1,2,3,4,5,6,7,8,9]
sample_of_three = random.sample(population, 3)

#print each element in list:
for each in sample_of_three:
    print(each)


append or pop.py

foods = ["Sushi", "Tacos", "Pizza", "Vegetable Soup"]

foods.append("Brussel Sprouts")

removal = foods.pop(2)

print(removal) #Pizza

print(foods) #['Sushi', 'Tacos', 'Vegetable Soup', 'Brussel Sprouts']



lists.py

colours = ['red', 'blue', 'green', 'yellow']
print(colours) #whole list
print(colours[0]) #red
print("Number of elements in list: ", len(colours)) #4
for colour in colours: #each item in list
    print(colour) #print each item

#------------------------------------------- 
days_in_sentence = "Monday Wednesday Friday"

#create list from string:
days_list = days_in_sentence.split()

#appending or changing an element:
print(days_list[-1]) #show last element in list
days_list.append("Saturday")
print(days_list[-1]) #show last element in list now
days_list[-1] = "Sunday"
print(days_list[-1])
days_list.reverse() #sorts back to front
print(days_list)
days_list.sort() #alphabetically sorts
print(days_list)


lists removing items.py

sushi = ['chicken', 'salmon', 'tofu', 'tuna']

#print all items:
for counter, sushiType in enumerate(sushi):
    print("Item number on menu:", str(counter))
    print("Sushi type:", sushiType)

sushi.remove('tofu') #removes first matching value (not specific index)
print(sushi)
#['chicken', 'salmon', 'tuna']

del sushi[2] #deletes item at index 2
print(sushi)
#['chicken', 'salmon']

myfave = sushi.pop(1) #removes the item at a specific index - and returns it
print(myfave) #salmon

print(sushi)
#['chicken']


nested list.py

#using a list of lists for a 3 x 3 grid:
nestedList =[["~" for x in range(3)] for y in range(3)]
nestedList[0][2] = "miss"
nestedList[1][0] = "hit"
#dont need to understand this yet:
print("\n".join(''.join(*zip(*row)) for row in nestedList))



  1. Create a new list of colours: colours = ['red','green','blue']. ask the user to input their favourite colour, then append the user input to the colours list.
  2. Create a new list of 3 foods: foods = ['sushi','pizza','burgers']
    • Ask the user to add their favourite food to the list using input and append
    • Ask the user to remove sushi from the list using remove('sushi')
    • Remove and store the first element from the list into a new variable called food (singular). To achieve this, use pop to remove the element that sits first on the list at index 0. Hint: pop(0)
    • You can also find the index of an element using index = foods.index('burgers'). What is the value of the index variable after this line of code is run? hint: use print

  3. We can input values into Python until the user enters an empty line, such as:
    movie = input('What movie have you watched? ') 
    while movie != '':
    Continuing this program, use the list append() function to create a unique list of movies the user has seen. You can check if a movie has been duplicated by using if and in together. When the list is complete, print it out in reverse alphabetical order. hint: use a combination of .sort() and .reverse()

  4. You are creating a list of outstanding employees. For each month of the year, append the first name of the "employee of the month" to the list. Make sure your favorite employee Benedict appears at least 3 times in the list. Once you have created the list, count the number of times Benedict appears in the list. Your program may look like this when it is run:
    employee award month 1: Sarah
    employee award month 2: Jane
    employee award month 3: Benedict
    employee award month 4: Louise
    employee award month 5: Jane
    employee award month 6: Benedict
    employee award month 7: Thomas
    employee award month 8: Benedict
    employee award month 9: Andrew
    employee award month 10: Rita
    employee award month 11: Hakeem
    employee award month 12: Benedict
    number of Benedict awards: 4

  5. Adjust your answer to the previous question to allow me to choose who I search the awards for:
    employee award month 1: Sarah
    employee award month 2: Jane
    employee award month 3: Benedict
    employee award month 4: Louise
    employee award month 5: Jane
    employee award month 6: Benedict
    employee award month 7: Thomas
    employee award month 8: Benedict
    employee award month 9: Andrew
    employee award month 10: Rita
    employee award month 11: Hakeem
    employee award month 12: Benedict
    Enter employee to count awards for: Jane
    number of Jane awards: 2