Beginner Python Cheat Sheet - Lists (Python Crash Course) Flashcards

1
Q

What does .insert() do and what are the arguments?

Source: http://ehmatthes.github.io/pcc/cheatsheets/README.html

A

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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does .pop() do and what are the arguments?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the keyword for deleting an element in a list by position?

A

It’s “del”

E.g.

del users[-1]

Del is also used to delete objects in general

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the keyword for deleting an element in a list by its value?

A

“remove”

E.g. users.remove(‘mia’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What function do you use to get the length of a list?

A

len()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you temporarily sort a list?

A

sorted()

E.g.
print(sorted(users, reverse=True))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you permanently sort a list?

A

.sort() method

E.g.
users.sort(reverse=True)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you reverse the order of a list?

A

.reverse() method

E.g.
users.reverse()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you copy a list so that changes to the new list don’t affect the original?

A

E.g.

finishers = ['kai', 'abe', 'ada', 'gus', 'zoe']
copy_of_finishers = finishers[:]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you write a list comprehension to capitalize all the letters in a list?

A

E.g.
names = [‘kai’, ‘abe’, ‘ada’, ‘gus’, ‘zoe’]
upper_names = [name.upper() for name in names]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly