This article describes in detail how to store dictionaries in the python3-list, for more information about how to store dictionaries in the python3-list, see the next article.
# Auther: Aaron Fan # Example 1: # define several dictionaries alien_0 = {"color": "green", "points": 5} alien_1 = {"color ": "yellow", "points": 10} alien_2 = {"color": "red", "points": 15} # store the dictionary in the list aliens. aliens = [alien_0, alien_1, alien_2] # traverse this list for alien in aliens: print (alien) # Example 2: # create an empty list for storing aliens = [] # create 30 green aliens for alien_number in range (30): new_alien = {"color": "green ", 'points': 5, 'speed': 'low'} aliens. append (new_alien) # display the first five aliens for alien in aliens [: 5]: print (alien) print ("... ") # display the total number of aliens created print (" The number of aliens is % d "% len (aliens) # Example 3: # create an empty list for storing aliens = [] # create 30 green aliens for alien_number in range (30): new_alien = {"color": "green ", 'points': 5, 'speed': 'low'} aliens. append (new_alien) for alien in aliens [0: 3]: if alien ['color'] = 'green ': alien ['color'] = 'yellow' alien ['speed'] = 'medium 'alien ['points'] = 10 elif alien ['color'] = 'yellow': alien ['color'] = 'red' alien ['speed'] = 'quick' alien ['points'] = 15 # display the first five aliens for alien in aliens [: 5]: print (alien) print ("... ")
The above is the details of the dictionary storage method in the python3-list. For more information, see other related articles in the first PHP community!