Lists Flashcards

1
Q

How to turn a string into a list?

A

string.split(x)

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

How to get ‘parakeet’ from all = [birds, pets] when birds = [‘parakeet’, ‘eagle’] and pets = [‘dogs’, ‘cats’]?

A

all[0][0]

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

Difference between list.append(x) and list.extend(x)/list +=x?

A

append puts list inside of x

extend/+= merges the two

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

How to append to list at position 3?

A

list.insert(3, x)

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

How to remove third item from a list?

A

del list[2]

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

What is it called when it’s del list[x] instead of something like list.del(x)?

A

del is called a statement

list.del is called a method

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

How to remove x from a list?

A

list.remove(x)

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

How to both delete and grab x?

A

list.pop(x)

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

How to make a first in, last out queue?

A

list.append(‘x’) > list.pop(‘x’). LIFO, work with newest first

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

How to make a first in, first out queue?

A

list.pop(x) where x is an integer. FIFO, work with oldest first

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

How to count number of times something appeared in a list?

A

list.count(x)

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

How to convert to a string?

A

string.join(list)

It’s a string method not a list method, that’s why the string is first

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

Difference between the functions sort(list) and sorted(list)?

A

sort(list) sorts list in place

sorted(list) makes a copy of the list, then sorts that

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

How to copy a list?

A

a=[1,2,3]
b = a.copy()
c = list(a)
d = a[:]

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

Difference between tuple and list?

A

You can’t change tuples

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

How to make a tuple?

A

a = (1, 2, 3)

17
Q

What can you do with tuples that you can’t with lists?

A

Use them as dictionary keys
named tuples
function arguments as tuples