Python Collections Flashcards
TF: Lists are mutable and strings are immutable:
True. When you change a list, it’s now different but it still takes up the same spot in the memory. A string however, when you change it, it takes up a new spot in memory.
This challenge is just meant as a refresher. Create a single new list variable named my_list. Put 3 numbers and 2 strings into it in whatever order you want. For the last member, though, add another list with 2 items in it.
my_list = [1, 2, 3, “Jon”, “Snow”, [5, 6]]
Create a list named best that has three items in it. The items can be anything you want.
Using .extend(), .append(), or +, add two new items onto the end of best. Again, the items can be anything you want.
Finally, use the .insert() method to place the string “start” at the beginning of your best list.
best = [1, 2, 3]
best. extend([4, 5])
best. insert(0, “start”)
TF: The + symbol is used to concatenate two lists together, just like with strings.
True
What does the first_list.extend(second_list) method do?
Adds the contents of a second list onto the first list
letters = [“A”, “B”, “C”]
letters.append([“D”, “E”, “F”])
What will letters be at the end of this?
[“A”, “B”, “C”, [“D”, “E”, “F”]]
I want my_list to have 1 as the last item.
Complete this method call:
My_list. _________ (1)
append
What does the list() function do?
Creates a new list & turns an iterable into a list.
What is the first argument to the .insert() method?
Index - the position in the list that you want something inserted
What are the 2 ways we can delete an item from the list?
alpha_list = [“A”, “B”, “C”, “D”, “E”]
Del
del alpha_list[2]
.remove()
alpha_list.remove(“D”)
If you use .remove, and there are multiple items in the list with the same name? What will happen? My_list = [1, 2, 3, 4, 1] my_list.remove(1)
It will remove the first 1 in the list and keep the second. So [2, 3, 4, 1]
What happens if we try to .remove() an item that doesn’t exist in the list?
We get a ValueError
What does ‘pass’ do?
A pass is essentially a “do nothing” statement.
OK, I need you to finish writing a function for me. The function disemvowel takes a single word as a parameter and then returns that word at the end.
I need you to make it so, inside of the function, all of the vowels (“a”, “e”, “i”, “o”, and “u”) are removed from the word. Solve this however you want, it’s totally up to you!
Oh, be sure to look for both uppercase and lowercase vowels!
def disemvowel(word): return word
def disemvowel(word): new_word = "" vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] for letters in word: if letters not in vowels: new_word += letters return new_word
What does .pop do?
.pop() will remove and return the last item from a list.
names.pop()
What do you have to use if you want to pop an item that isn’t the last one in the list?
.pop(index) will remove whatever index the item is at, assuming something is.
names.pop(0)
Alright, my list is messy! Help me clean it up!
First, start by moving the 1 from index 3 to index 0. Try to do this in a single step by using both .pop()and .insert(). It’s OK if it takes you more than one step, though!
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]] # Your code goes below here
Great! Now use .remove() and/or del to remove the string, the boolean, and the list from inside of messy_list. When you’re done, messy_list should have only integers in it.
messy_list.pop(3)
messy_list.insert(0, 1)
messy_list.remove(“a”)
messy_list.remove(False)
messy_list.remove([1, 2, 3])
What is a slice?
A slice is a new list or string made from another list or string.
If you make a slice from a list, you get a list.
If you make a slice from a string, you get a string.
[start:stop]
Favorite_things[1:4]
What happens when you don’t provide a value for the start or end off of a slice?
Both start and stop are optional. If you leave off start, the slice will start at the beginning of the iterable. If you leave off stop, the slice will continue until the end of the iterable.
Create a new variable named slice1 that has the second, third, and fourth items from favorite_things.
favorite_things = [‘raindrops on roses’, ‘whiskers on kittens’, ‘bright copper kettles’, ‘warm woolen mittens’, ‘bright paper packages tied up with string’, ‘cream colored ponies’, ‘crisp apple strudels’]
OK, let’s do another test. Get the last two items from favorite_things and put them into slice2.
Make a copy of favorite_things and name it sorted_things.
Then use .sort() to sort sorted_things.
slice1 = favorite_things[1:4]
slice2 = favorite_things[5:]
sorted_things = favorite_things[:]
sorted_things.sort()