Skip to content

Instantly share code, notes, and snippets.

@fitoprincipe
Created May 29, 2019 01:12
Show Gist options
  • Save fitoprincipe/2eed39d6c6df5e4bdb0eabb234bed804 to your computer and use it in GitHub Desktop.
Save fitoprincipe/2eed39d6c6df5e4bdb0eabb234bed804 to your computer and use it in GitHub Desktop.
# first list
a = [1, 2, 3, 4]
# second list
b = ['a', 'b']
# expected list
exp = ['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4']
# WITHOUT list comprehension
result = []
for item_a in a:
for item_b in b:
result.append(item_b+str(item_a))
print('without', result)
# WITH list comprehension
result = [item_b+str(item_a) for item_a in a for item_b in b]
print('with', result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment