Skip to content

Instantly share code, notes, and snippets.

@jan-matejka
Created June 29, 2012 12:31
Show Gist options
  • Select an option

  • Save jan-matejka/3017652 to your computer and use it in GitHub Desktop.

Select an option

Save jan-matejka/3017652 to your computer and use it in GitHub Desktop.
left-right comprehensions
>>> a = [['a', 'b'], ['c','d']]
>>> [["1_%s" % x for x in xs] for xs in a] # nested nested comprehension keeps the input nest
[['1_a', '1_b'], ['1_c', '1_d']]
>>> ["1_%s" % x for xs in a for x in xs] # flat nested comprehension flattens the input
['1_a', '1_b', '1_c', '1_d']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment