Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Python: List & dictionary comprehension

📅 2009-Oct-13 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

  Lists

alist = [1, 2, 3]
blist = [x*x for x in alist]    # [1, 4, 9]

# Dictionaries
adict = {3:"three", 5:"five", 1:"one"}
bdict = {num:None for num in adict} # {1: None, 3: None, 5: None}