Beginner Python Cheat Sheet - Lists (Python Crash Course) Flashcards
What does .insert() do and what are the arguments?
Source: http://ehmatthes.github.io/pcc/cheatsheets/README.html
The first argument is an integer indicating position. The second is the item to insert into the list. If you want to add to the end, then use ‘append’.
What does .pop() do and what are the arguments?
pop removes the last item of the list so you can work with it as a variable. The argument is optional and is a integer that indicates a position in the list that you would like to pop.
What is the keyword for deleting an element in a list by position?
It’s “del”
E.g.
del users[-1]
Del is also used to delete objects in general
What is the keyword for deleting an element in a list by its value?
“remove”
E.g. users.remove(‘mia’)
What function do you use to get the length of a list?
len()
How do you temporarily sort a list?
sorted()
E.g.
print(sorted(users, reverse=True))
How do you permanently sort a list?
.sort() method
E.g.
users.sort(reverse=True)
How do you reverse the order of a list?
.reverse() method
E.g.
users.reverse()
How do you copy a list so that changes to the new list don’t affect the original?
E.g.
finishers = ['kai', 'abe', 'ada', 'gus', 'zoe'] copy_of_finishers = finishers[:]
How would you write a list comprehension to capitalize all the letters in a list?
E.g.
names = [‘kai’, ‘abe’, ‘ada’, ‘gus’, ‘zoe’]
upper_names = [name.upper() for name in names]