Python3-how to store dictionaries in the list

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.