There will be many Dictionaries are stored in lists in , Or will List as value Store in a dictionary .
establish 3 A dictionary alien_0,alien_1,alien_2; Store the dictionary in a list aliens in :
# Create a dictionary
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':10}
alien_2={'color':'red','points':15}
# Nested dictionaries in the list
aliens=[alien_0,alien_1,alien_2]
# Print
for alien in aliens:
print(alien)
Running results :
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}
Create more dictionaries , Save list aliens in :
aliens=[];
for aliens_num in range(0,10):
new_aliens={'color':'green','point':5,'speed':'slow'}
aliens.append(new_aliens)
for alien in aliens[:4]:# Before printing 4 individual aliens
print(alien)
print('.....')
print(f" Altogether {len(aliens)} individual alien")
Running results :
{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
.....
Altogether 10 individual alien
establish , Nested list in dictionary :
vehicle_info={'Number':1001,
'variable':['nmot_w','rl_w','miist_w']}
print(f"Vehicle {vehicle_info['Number']} measured variable: {vehicle_info['variable']}")
Running results :
Vehicle 1001 measured variable: ['nmot_w', 'rl_w', 'miist_w']
Nested dictionaries in the list , Nested list in dictionary :
# Nested list in dictionary
vehicle_info_0={'Number':1001,
'variable':['nmot_w','rlsol_w','miist_w']}
vehicle_info_1={'Number':1002,
'variable':['nmot_w','rl_w','miist_w']}
vehicle_info_2={'Number':1003,
'variable':['nmot_w','rlp_w','miist_w']}
vehicle_info_3={'Number':1004,
'variable':['nmot_w','rl_w','miist_w']}
# Nested dictionaries in the list
vehicle_infos=[vehicle_info_0,vehicle_info_1,vehicle_info_2,vehicle_info_3]
for vehicle_info in vehicle_infos:# Traverse the list of vehicles
for value in vehicle_info['variable']:# Traverse the measured variables in the vehicle list
if value == 'rl_w':# Look for something that contains rl_w Variable vehicle , And print it out
print(f"rl_w is within the {vehicle_info['Number']} measurement variable.")
Running results :
rl_w is within the 1002 measurement variable.
rl_w is within the 1004 measurement variable.
Dictionary nested dictionary :
# Nested list in dictionary
vehicle_info_0={'Number':1001,
'variable':['nmot_w','rlsol_w','miist_w']}
vehicle_info_1={'Number':1002,
'variable':['nmot_w','rl_w','miist_w']}
vehicle_info_2={'Number':1003,
'variable':['nmot_w','rlp_w','miist_w']}
vehicle_info_3={'Number':1004,
'variable':['nmot_w','rl_w','miist_w']}
# Nested dictionaries in dictionaries
vehicle_infos= {
'jack':vehicle_info_0,
'allen':vehicle_info_1,
'lucy':vehicle_info_2,
'jhon':vehicle_info_3
}
for vehicle_info in vehicle_infos.keys():# Traverse the vehicle owner key
if vehicle_infos[vehicle_info]['Number'] == 1003:# seek 1003 Number of the vehicle owner , And print it out
print(f"Vehicle 1003 is belone to {vehicle_info} .")
Running results :
Vehicle 1003 is belone to lucy .