Skip to content

Instantly share code, notes, and snippets.

@H3lllfir3
Last active November 6, 2019 13:09
Show Gist options
  • Select an option

  • Save H3lllfir3/4c769f513675f2f2bdaf7b61ef61141c to your computer and use it in GitHub Desktop.

Select an option

Save H3lllfir3/4c769f513675f2f2bdaf7b61ef61141c to your computer and use it in GitHub Desktop.
Dictionary Comprehension
>>> numbers = {num:num ** 2 for num in range(5)}
>>> numbers
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> numbers = {x:x ** 2 for x in [1, 2, 3, 4, 5, 6] if x % 2 == 0}
>>> numbers
{2: 4, 4: 16, 6: 36}
>>> names = ['ayoub', 'reza', 'mersad']
>>> names = {f:f.capitalize() for f in names}
{'ayoub': 'Ayoub', 'reza': 'Reza', 'mersad': 'Mersad'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment