Chapter 7 Flashcards

1
Q

What does list.append() do?

A

Add the contents of the brackets to the end of the list as one element

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

What does list.extend() do?

A

Add a list to the end of the list

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

What does list[x:x] = [1,2,3] do?

A

Add the list [1,2,3] at index x, move every value past index x down by one (including index x)

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

what does list[x] = 1 do?

A

replace list[x] current value with 1

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

What does list.insert(index,value) do?

A

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]

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

What does list.insert(len(list),val) do?

A

list.append(val)

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

What is the output for x[::-1]
x = [1, 2, 3, 4, 5, 6]

A

[6, 5, 4, 3, 2, 1]

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

How do you splice a list?

A

list[start : end : index step]
start included
end not included

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

What does list[:] output?

A

The complete list

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

What does list[::-2] output?

A

Every other element of the list, counting backwards

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

What does list[-1] output?

A

Last character of the list

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

What does list[-1:] output?

A

The last character of a list

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

What does list.pop(index) do?

A

Deletes the value at the index in the list, can also be assigned to a variable while removing it from a list

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

What does list.sort() do?

A

Sort the list in ascending order

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

What does list.index(x) do?

A

returns the earliest index of value x

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

What does del list[x] do?

A

Deletes the value in index x, shuffles everything in the list down an index

17
Q

How do you access elements in a dictionary?

A

element = dictionary[key]

18
Q

How do you loop through each key of a dictionary?

A

for key in dictionary:

19
Q

How do you get a value from a list in a dictionary?

A

dictionary[key][index in list]

e.g:
d = {‘x’ : 9,’y’ : 5, ‘z’ : [1,2,3]}
print(d[‘z’][2])

output:
3

20
Q

What does dictionary.get(x) do?

A

Get the value from key x in a dictionary

21
Q

How do you get each key:value pair form a dictionary?

A

for key, value in dictionary.items():
print(f’{key}:{value}’)

output:
key:value
….
….

22
Q

What does dictionary.items() do?

A

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

23
Q

How do you add a key:value pair to a dictionary?

A

dict[key] = value,
if key already exists, override current value

24
Q

How do you delete a key:value pair?

A

del dict[key]

25
Q

how do you edit a value in a dictionary?

A

dict[key] = new_value

26
Q

Can you do mathematical operations within a dictionary?

A

yes for example
a[‘c’] = 3
a[‘c’] +=3
print(a[‘c’])
output = 6

27
Q

What is the output?

mystr = “Howdy”
mylist = [2, 0, 2, 0]
mytuple = (mystr, mylist)
mylist[3] = 6
print(mytuple)

A

(‘Howdy’, [2, 0, 2, 6])
- The list is attached to the index, not the values of the list

28
Q

What does int(‘5.0’) result in?

A

It produces an error, ‘.’ cannot be converted into an integer

29
Q

Can you have a float in the range() function?

A

No, only integer values,
- Use a while loop if you want a floating step

30
Q

How do you use the range function to create a list?

A

a = list(range(a,b,c))

31
Q

What does list.remove(x) do?

A

Remove the earliest occurrence of value x from the list

32
Q

What are the restrictions on data types for a dictionary key?

A

Keys can only be any immutable data type:
- string
- tuple

33
Q

Which of these are tuples?
(1,2,3)
1, 2, 3
[1, 2, 3]

A

(1, 2, 3)
1, 2, 3

34
Q

How do you print a list or tuple without brackets or commas?

A

print(tuple)
print(
list)

35
Q

How can you remove sections of a list?

A

a = [1,2,3,4,5,6,7]
a[:3] = []
print(a)
output = [4,5,6,7]