4 Lists Flashcards

1
Q
A = [ [ A, B, C], [1, 2, 3] ]
B = [ 9, 8, 7, 6, 5 ]

How many list values and items are in A?
What is A [0][1]?

What is the index range of B?

A

A = [ [ A, B, C], [1, 2, 3] ]
-> 2x list values
-> 2x lists with 3 items each
A [0][1] -> 0 indicates the first list value ABC, 1 indicates the index of that list, which is index and thus item B

B = [ 9, 8, 7, 6, 5 ]
-> index range 0 to 4

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

In a slice the first integer is the index where the slice […]. The second integer is where the slice […]. A slice […], but will not […] the value at the second index.

A

In a slice the first integer is the index where the slice [starts]. The second integer is where the slice [ends]. A slice [goes up to], but will not [include] the value at the second index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A = [ A, B, C]
How do you:
    replace
    add
    delete items from a list
A

A = [ A, B, C]
Replace:
A[0] = ‘D’

Add:
A.append(‘D’)
A.insert(‘2, D’)

Delete:
A.remove(‘D’) -> When you know the value
del A[0] -> When you know the index

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

A= [‘pens’, ‘staplers’, ‘flamethrowers’]

How can a for loop access and print out the indexes of a loop combined with their item value? (2x)

A

1.
A= [‘pens’, ‘staplers’, ‘flamethrowers’]
for x in range(len(A) ):
print( str(x) + A[x] )

Output:
0pens
1staplers
2flamethrowers

  1. enumerate() function returns two values: The first one is the index of the item, the second one the item itself
    for index, item in enumerate(A):
    print(str(index) + item)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you find out if a certain value is in a list?

-> myPets example

A

With the ‘in’ and ‘not in ‘ operators which will return False or True

myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name:')
name = input()
if name not in myPets:
    print('I do not have a pet named ' + name)
else:
    print(name + ' is my pet.')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Multiple Assignment Trick, what is a preqrequisite for it and how is it called technically?

A

Instead of assigning values to items in a list one by one, they can be assigned simulteanously.
Preqrequisite : number of variables and length of list must be equal
Technical term: Tuple Unpacking

> > > cat = [‘fat’, ‘gray’, ‘loud’]
size, color, disposition = cat
size
‘fat’

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

What is the long version of:
»> import random
»> random.choice(listA)

How can you randomly reorder items in a list?

A

listA[random.randint(0, len(listA)-1)]

Reorder:
random.shuffle(listA)

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

Name the 5 Augmented Assignment Operators

A
spam += 1
spam -= 1
spam *= 1
spam /= 1
spam %= 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
>>> bacon = 'Hello'
>>> id(bacon)
44491136
>>> bacon += ' world!' 
>>> id(bacon) 

What happens to the ID of bacon and why?

A

Strings are immutable. Thus, the “change” leads to the creation of a new string with a new ID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
>>> bacon = ['cat', 'dog'] 
>>> id(bacon )
35152584
>>> bacon .append('moose') # modifies the list "in place".
>>> id(bacon ) 

What happens to the ID of bacon and why?

A

Lists are mutable. So the object is “modified in place”, altering the existing object instead of creating a new one. The ID stays the same.

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

What can be modified in place and how?

A

Mutable data types, such as lists.
For that you can use list methods, e.g.
sort(), remove(), extend(), append(), reverse()

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

What is the purpose of Python´s copy.module?

A

When a function is called, the values of the arguments are copied to the parameter variables. For lists & dict´s this means a copy of the reference is used for the parameter.
To create entirely new lists, and not copies to former references, you can use the copy module.

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

What is the difference between copy and deepcopy?

A
copy = for lists
deepcopy = for lists that contain lists as well
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a tuple

A

A tuple is an immutable sequence data type. In contrast to lists it is written with () instead of []
egg = (A, B, C)

A tuple with a single item is distinct from a strin because of its ‘,’
egg = (‘hello’,) instead of ‘hello’

just like all immutable data types, tuples cannot be changed

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

What are lists?

A

Lists are a mutable sequence data type, indicated by = [ ]. Variables can store references to lists (not list values directly!!!).

Changes to a list might change other variables in the code

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

How would you assign the value ‘hello’ as the third value in a list stored in a variable named spam? (Assume spam contains [2, 4, 6, 8, 10].)

A

spam[2] = ‘hello’

17
Q

Imagine spam = [a, b, c, d]

  1. What does spam[int(int(‘3’ * 2) // 11)] evaluate to?
  2. What does spam[-1] evaluate to?
  3. What does spam[:2] evaluate to?
A

Imagine spam = [a, b, c, d]
1. What does spam[int(int(‘3’ * 2) // 11)] evaluate to?
33 / 11 = 3 -> spam[3] -> ‘d’
2. What does spam[-1] evaluate to?
‘d’
3. What does spam[:2] evaluate to?
‘a’, ‘b’ –> up to 2, so ‘c’ is not included!

18
Q

Imagine bacon = [3.14, ‘cat’, 11, ‘cat’, True]

  1. What does bacon.index(‘cat’) evaluate to?
  2. What does bacon.append(99) make the list value in bacon look like?
  3. What does bacon.remove(‘cat’) make the list value in bacon look like?
A

Imagine bacon = [3.14, ‘cat’, 11, ‘cat’, True]

  1. What does bacon.index(‘cat’) evaluate to?
    ‘1’
  2. What does bacon.append(99) make the list value in bacon look like?
    bacon = [3.14, ‘cat’, 11, ‘cat’, True, 99]
  3. What does bacon.remove(‘cat’) make the list value in bacon look like?
    bacon = [3.14, 11, ‘cat’, True, 99]
19
Q

What is the difference between the append() and insert() list methods?

A

append() adds to the end of the list

insert() adds to the specified index

20
Q

Name a few ways that list values are similar to strings

A

Strings start with ‘’, lists with [ ]
Strings store series of letters, lists of items
both letters and items can be accessed with egg[i]
both can be *multiplied or +concatenated

21
Q

How do you type the tuple value that has just the integer value 42 in it?

A

egg = (42,)

-> the , is important!

22
Q

How can you get the tuple form of a list value? How can you get the list form of a tuple value?

A

> > > tuple([‘cat’, ‘dog’, 5])
(‘cat’, ‘dog’, 5)
list((‘cat’, ‘dog’, 5))
[‘cat’, ‘dog’, 5]