xxxxxxxxxx
# This is our example dictionary
petAges = {"Cat": 4, "Dog": 2, "Fish": 1, "Parrot": 5}
# This will be our list, epmty for now
petAgesList = []
# Search through the dictionary and find all the keys and values
for key, value in petAges.items():
petAgesList.append([key, value]) # Add the key and value to the list
print(petAgesList) # Print the now-filled list
# Output: [['Cat', 4], ['Dog', 2], ['Fish', 1], ['Parrot', 5]]
xxxxxxxxxx
for key, value in dict.iteritems():
temp = [key,value]
dictlist.append(temp)
xxxxxxxxxx
#dictionary of lists to list of dictionaries:
v = [dict(zip(DL,t)) for t in zip(*DL.values())]
print(v)
# and back
v = {k: [dic[k] for dic in LD] for k in LD[0]}
print(v)
xxxxxxxxxx
>> d = {'a': 'Arthur', 'b': 'Belling'}
>> d.items()
[('a', 'Arthur'), ('b', 'Belling')]
>> d.keys()
['a', 'b']
>> d.values()
['Arthur', 'Belling']
xxxxxxxxxx
new_player1 = { 'firstName': 'LaMarcus', 'lastName': 'Aldridge', 'jersey': '12', 'heightMeters': '2.11', 'nbaDebutYear': '2006', 'weightKilograms': '117.9'}
new_player2 = { 'firstName': 'LeBron', 'lastName': 'James', 'jersey': '2', 'heightMeters': '2.03', 'nbaDebutYear': '2003', 'weightKilograms': '113.4' }
new_player3 = { 'firstName': 'Kawhi', 'lastName': 'Leonard', 'jersey': '2', 'heightMeters': '2.01', 'nbaDebutYear': '2011', 'weightKilograms': '104.3' }
nba_players = []
nba_players.append(player)
nba_players.append(new_player1)
nba_players.append(new_player2)
nba_players.append(new_player3)
xxxxxxxxxx
single_dict = dict((k,v) for k,v in [item for subtup in list_of_dict for item in subtup])
xxxxxxxxxx
lst = [{'a':0,'b':1,'c':2},
{'d':3,'c':4,'b':5},
{'a':5,'d':6,'e':1}]
print(lst[1])
print(lst[1]['c'])
xxxxxxxxxx
list_of_dict = []
for row in list_of_list[1:]: # Taking 0 index as header or key and rest as value
list_of_dict.append({key: val for key, val in list(zip(*[list_of_list[0], row]))})