Python Accessing Values in A List of Dictionaries

后端 未结 6 1379
时光说笑
时光说笑 2020-12-24 04:51

Say I have a List of dictionaries that have Names and ages and other info, like so:

thisismylist= [  
              {\'Name\': \'Albert\' , \'Age\': 16},
          


        
6条回答
  •  悲&欢浪女
    2020-12-24 05:10

    Looks like you need to go over the Python flow-control documentation. Basically, you just loop over all the items in your list, and then for each of those items (dictionaries, in this case) you can access whatever values you want. The code below, for instance, will print out every value in every dictionary inside the list.

    for d in my_list:
        for key in d:
            print d[key]
    

    Note that this doesn't print the keys, just the values. To print the keys as well, make your print statement print key, d[key]. That easy!

    But really, go read the flow-control documentation; it's very nice.

提交回复
热议问题