Chapter 7 Flashcards
What does list.append() do?
Add the contents of the brackets to the end of the list as one element
What does list.extend() do?
Add a list to the end of the list
What does list[x:x] = [1,2,3] do?
Add the list [1,2,3] at index x, move every value past index x down by one (including index x)
what does list[x] = 1 do?
replace list[x] current value with 1
What does list.insert(index,value) do?
inserts the value in list[index] while shuffling everything down one index, nothing is overwritten.
- Though .insert() does nest its contents, it doesn’t split it into separate indices
- e.g. list = [1,2,5,6]
- list.instert(2, [3,4])
- list = [1,2, [3,4], 5, 6]
What does list.insert(len(list),val) do?
list.append(val)
What is the output for x[::-1]
x = [1, 2, 3, 4, 5, 6]
[6, 5, 4, 3, 2, 1]
How do you splice a list?
list[start : end : index step]
start included
end not included
What does list[:] output?
The complete list
What does list[::-2] output?
Every other element of the list, counting backwards
What does list[-1] output?
Last character of the list
What does list[-1:] output?
The last character of a list
What does list.pop(index) do?
Deletes the value at the index in the list, can also be assigned to a variable while removing it from a list
What does list.sort() do?
Sort the list in ascending order
What does list.index(x) do?
returns the earliest index of value x
What does del list[x] do?
Deletes the value in index x, shuffles everything in the list down an index
How do you access elements in a dictionary?
element = dictionary[key]
How do you loop through each key of a dictionary?
for key in dictionary:
How do you get a value from a list in a dictionary?
dictionary[key][index in list]
e.g:
d = {‘x’ : 9,’y’ : 5, ‘z’ : [1,2,3]}
print(d[‘z’][2])
output:
3
What does dictionary.get(x) do?
Get the value from key x in a dictionary
How do you get each key:value pair form a dictionary?
for key, value in dictionary.items():
print(f’{key}:{value}’)
output:
key:value
….
….
What does dictionary.items() do?
finds all key:value pairs in a dictionary,
- Can be used in a loop
- If assigned to a variable it will print a tuple called ‘dict_items’ filled with a list of each key:value pair
How do you add a key:value pair to a dictionary?
dict[key] = value,
if key already exists, override current value
How do you delete a key:value pair?
del dict[key]