What is a list [ ]
A collection of items stored in a variable using brackets [].
What does this print?
animals = ["cat","dog","mouse"]
print(animals[2])
mouse
What does the list method .append do?
animals.append("horse")
adds horse to the end of a list
What does this do?
Removes "cat" from the list.
pets = ["cat","dog"]
pets.append("fish")
print(pets)
["cat","dog","fish"]
What does this code print?
animals = ["cat","dog","mouse"]
print(animals)
["cat","dog","mouse"]
What does this print?
animals = ["cat","dog","mouse"]
animals[1] = "cow"
print(animals)
["cat","cow","mouse"]
What does .insert do?
animals.insert(2,"pig")
Adds "pig" at index 2.
What does this do?
animals.pop(2)
Removes the item at index 2.
pets = ["cat","dog","mouse"]
pets.pop(1)
print(pets)
["cat","mouse"]
How do you access the first item in a list?
list[0]
Which index refers to "dog"?
1
What does this do?
Adds all items from list2 to list1.
What happens?
print(x)
Removes the item at an index 2, stores it in x, prints x
pets = ["cat","dog","mouse"]
pets.remove("dog")
print(pets)
["cat","mouse"]
What does this print?
animals = ["cat","dog","mouse"]
print(animals[1])
dog
What does this print?
nums = [10,20,30,40]
print(nums[-1])
40
What is the output?
nums = [1,2,3]
nums.append(4)
[1,2,3,4]
What is the difference between .remove() and .pop()?
remove() removes by value
pop() removes by index
pets = ["cat","dog","mouse"]
pets.insert(1,"bird")
print(pets)
["cat","bird","dog","mouse"]
True or False: Lists can store multiple data items.
True
What does negative indexing do?
What is the difference between .insert() and .append()?
append() adds to end
insert() adds at a specific index
What does this do?
if "lion" in animals:
animals.remove("lion")
Check if "lion" is in the list first and then removes it.
pets = ["cat","dog","mouse"]
x = pets.pop(1)
print(x)
dog