Dictionary Comprehensions
We'll cover the following...
A dictionary comprehension is like a list comprehension, but it constructs a dictionary instead of a list.
Press + to interact
import os, globmetadata = [(f, os.stat(f)) for f in glob.glob('*test*.py')] #①print (metadata[0] ) #②# ('romantest3.py', os.stat_result(st_mode=33261,# st_ino=1057919, st_dev=2049, st_nlink=1, st_uid=1003,# st_gid=50, st_size=4640, st_atime=1472210494,# st_mtime=1472151083, st_ctime=1472210494))metadata_dict = {f:os.stat(f) for f in glob.glob('*test*.py')} #③print (type(metadata_dict)) #④#<class 'dict'>print (list(metadata_dict.keys())) #⑤#['romantest2.py', 'romantest3.py', 'romantest1.py']print (metadata_dict['romantest3.py'].st_size) #⑥#4640
① This is not a dictionary comprehension; it’s a list comprehension. It finds all .py
files with test
in their name, then constructs a tuple of the filename ...