4 Lists Flashcards
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, 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
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.
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.
A = [ A, B, C] How do you: replace add delete items from a list
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
A= [‘pens’, ‘staplers’, ‘flamethrowers’]
How can a for loop access and print out the indexes of a loop combined with their item value? (2x)
1.
A= [‘pens’, ‘staplers’, ‘flamethrowers’]
for x in range(len(A) ):
print( str(x) + A[x] )
Output:
0pens
1staplers
2flamethrowers
- 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 can you find out if a certain value is in a list?
-> myPets example
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.')
What is the Multiple Assignment Trick, what is a preqrequisite for it and how is it called technically?
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’
What is the long version of:
»> import random
»> random.choice(listA)
How can you randomly reorder items in a list?
listA[random.randint(0, len(listA)-1)]
Reorder:
random.shuffle(listA)
Name the 5 Augmented Assignment Operators
spam += 1 spam -= 1 spam *= 1 spam /= 1 spam %= 1
>>> bacon = 'Hello' >>> id(bacon) 44491136 >>> bacon += ' world!' >>> id(bacon)
What happens to the ID of bacon and why?
Strings are immutable. Thus, the “change” leads to the creation of a new string with a new ID
>>> 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?
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.
What can be modified in place and how?
Mutable data types, such as lists.
For that you can use list methods, e.g.
sort(), remove(), extend(), append(), reverse()
What is the purpose of Python´s copy.module?
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.
What is the difference between copy and deepcopy?
copy = for lists deepcopy = for lists that contain lists as well
What is a tuple
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
What are lists?
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