Lists Flashcards
How to define a list?
list = [10, Haus, 55, Katze] (mix between INT / string)
How to add a single item to the list?
list.append(50)
How to add multiple items to a list?
list.extend(50,60,70)
Item count in a list?
len(list)
How to remove item from a list?
list.pop(0)
How to define a 2-D list?
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
How to access “2” or “4”?
matrix[0][1]
matrix[1][0]
How do you define a dictionary?
person = {‘Name’: “Fergus”,
‘Age’: 34,
‘Skills’: [“Python”, “Perl”, “SQL”]
}
person = {‘Name’: “Fergus”,
‘Age’: 34,
‘Skills’: [“Python”, “Perl”, “SQL”]
}
How to access items?
person[‘Name’] # returns “Fergus”
person[‘Age’] # returns “34”
Dictionaries: How to return…
all keys
all items
all keys & items
dict. keys()
dict. values()
dict. items()
How to add a new item in position 0?
list.insert(0,newitem)
How to update an item to a dictionary?
my_dict[‘key1’] = my_dict[‘key1’] - 123
How to add an item to a dictionary?
mydict[‘key’] = ipaddr
How to print out whole dictionary in pairs?
for key in my_ips.keys():
print(key + “\t” + str(my_ips[key]))
How to check if key is already present in dictionary?
if ipadr in my_ips.keys():
do something….