Skip to content

Instantly share code, notes, and snippets.

@MaiLinhGroup
Last active August 31, 2021 13:39
Show Gist options
  • Save MaiLinhGroup/a6c29f61f22f58f5246bfb82b3e7ff44 to your computer and use it in GitHub Desktop.
Save MaiLinhGroup/a6c29f61f22f58f5246bfb82b3e7ff44 to your computer and use it in GitHub Desktop.
# dictionary comprehension
dict_original = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
my_new_dict = {k:v for (k,v) in dict_original.items()}
# list comprehension
list_original = ['h', 'u', 'm', 'a', 'n']
my_new_list = [letter+letter for letter in list_original] #[expression for item in list]
print(my_new_list) # ['hh', 'uu', 'mm', 'aa', 'nn']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment