Lists Flashcards

1
Q

How to define a list?

A

list = [10, Haus, 55, Katze] (mix between INT / string)

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

How to add a single item to the list?

A

list.append(50)

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

How to add multiple items to a list?

A

list.extend(50,60,70)

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

Item count in a list?

A

len(list)

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

How to remove item from a list?

A

list.pop(0)

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

How to define a 2-D list?

A

matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

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

matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

How to access “2” or “4”?

A

matrix[0][1]

matrix[1][0]

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

How do you define a dictionary?

A

person = {‘Name’: “Fergus”,
‘Age’: 34,
‘Skills’: [“Python”, “Perl”, “SQL”]
}

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

person = {‘Name’: “Fergus”,
‘Age’: 34,
‘Skills’: [“Python”, “Perl”, “SQL”]
​}

How to access items?

A

person[‘Name’] # returns “Fergus”
person[‘Age’] # returns “34”

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

Dictionaries: How to return…
all keys
all items
all keys & items

A

dict. keys()
dict. values()
dict. items()

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

How to add a new item in position 0?

A

list.insert(0,newitem)

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

How to update an item to a dictionary?

A

my_dict[‘key1’] = my_dict[‘key1’] - 123

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

How to add an item to a dictionary?

A

mydict[‘key’] = ipaddr

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

How to print out whole dictionary in pairs?

A

for key in my_ips.keys():
print(key + “\t” + str(my_ips[key]))

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

How to check if key is already present in dictionary?

A

if ipadr in my_ips.keys():
do something….

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