- Back to Home »
- python - List and Dictionary comprehension
Posted by : ANIL KUMAR MULA
Friday, 2 January 2015
python comprehension is a good construct with that code is compressed to a greater extent. here is a sample example given for that.

#print even numbers from 1 to 10; 2 4 6 8
print([i for i in range(1,11) if i % 2 == 0])
#print dictionary; key as key and value as length of key
print({i:len(i) for i in ["python","ruby","c"] })

#print even numbers from 1 to 10; 2 4 6 8
print([i for i in range(1,11) if i % 2 == 0])
#print dictionary; key as key and value as length of key
print({i:len(i) for i in ["python","ruby","c"] })