Home > Blockchain >  How do I take an item that is inside a list and put it in a variable?
How do I take an item that is inside a list and put it in a variable?

Time:10-28

I have this list, but I want to work with the 'file_type', 'access_time', 'modification_time', 'blocksize' and 'path' keys. How do I just get these values ​​from within a list so I can put each one into a variable?

This list corresponds to the return of the request for information about a certain file and I need to take specific values ​​from specific keys and store them in a variable.

list = [{'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1367317324982L, 'block_replication': 1, 'modification_time': 1367317325346L, 'length': 6783L, 'blocksize': 134217728L, 'owner': u'wouter', 'path': '/Makefile'}]

I need to strip the values ​​from each key and insert them into a variable so I can work with them better.

For example:

access = access_time value
modification = modification_time value

etc

CodePudding user response:

list_of_dicts = [YOUR DATA HERE]

for d in list_of_dicts:
    file_type         = d['file_type']
    access_time       = d['access_time']
    modification_time = d['modification_time']
    blocksize         = d['blocksize']
    path              = d['path']
    
    # do your stuff here, for each dict in your starting list
  • Related