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]
how do you edit a value in a dictionary?
dict[key] = new_value
Can you do mathematical operations within a dictionary?
yes for example
a[‘c’] = 3
a[‘c’] +=3
print(a[‘c’])
output = 6
What is the output?
mystr = “Howdy”
mylist = [2, 0, 2, 0]
mytuple = (mystr, mylist)
mylist[3] = 6
print(mytuple)
(‘Howdy’, [2, 0, 2, 6])
- The list is attached to the index, not the values of the list
What does int(‘5.0’) result in?
It produces an error, ‘.’ cannot be converted into an integer
Can you have a float in the range() function?
No, only integer values,
- Use a while loop if you want a floating step
How do you use the range function to create a list?
a = list(range(a,b,c))
What does list.remove(x) do?
Remove the earliest occurrence of value x from the list
What are the restrictions on data types for a dictionary key?
Keys can only be any immutable data type:
- string
- tuple
Which of these are tuples?
(1,2,3)
1, 2, 3
[1, 2, 3]
(1, 2, 3)
1, 2, 3
How do you print a list or tuple without brackets or commas?
print(tuple)
print(list)
How can you remove sections of a list?
a = [1,2,3,4,5,6,7]
a[:3] = []
print(a)
output = [4,5,6,7]