1. simple list.py
foods = ["Sushi", "Tacos", "Pizza", "Vegetable Soup"] print(foods[2]) #Pizza
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']
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)
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']
#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))
append()
pop
this food out of the list and return it to the user as output. print the remaining items in the list.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.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
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