Skip to content

Instantly share code, notes, and snippets.

@caffeine-potent
Last active January 28, 2018 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caffeine-potent/6a3324436e722c4571e51a5c5b60d890 to your computer and use it in GitHub Desktop.
Save caffeine-potent/6a3324436e722c4571e51a5c5b60d890 to your computer and use it in GitHub Desktop.
List comprehension with depth = 2
#
# Solution to a question posed in https://www.reddit.com/r/learnpython/comments/72ll3i/list_comprehension_help/
#
# def gen_function_that_can_be_one_lined(my_list):
# for i, x in enumerate(my_list):
# for y in x:
# yield ( i, y)
#
#List
example_1 = [( i, y) for i, x in enumerate(my_list) for y in x]
#Generator
example_2 = (( i, y) for i, x in enumerate(my_list) for y in x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment