Created
May 29, 2019 01:12
-
-
Save fitoprincipe/2eed39d6c6df5e4bdb0eabb234bed804 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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