Python Tutorial

Friday, December 20, 2013

python dictionary comprehensions

Python dictionary comprehensions is similar to list comprehension. Lets have some example
import os

# from a directory get each file name as key and file size as value
print {filename: os.path.getsize(filename) for filename in os.listdir(os.getcwd()) }

# initialize from 0-9 key as true
print {n: True for n in range(10)}

0 comments:

Post a Comment