I was playing with list comprehensions, to try and work out how doubled
up versions work (like this one from another thread: [i for i in
range(9) for j in range(i)]). I think I've figured that out, but I
found something strange along the way:
[color=blue][color=green][color=darkred]
>>> alpha = ["one", "two", "three"]
>>> beta = ["A", "B", "C"]
>>> [x for x in alpha for y in beta][/color][/color][/color]
['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three'][color=blue][color=green][color=darkred]
>>> [x for x in y for y in beta][/color][/color][/color]
['C', 'C', 'C'][color=blue][color=green][color=darkred]
>>> beta = [alpha, alpha, alpha]
>>> beta[/color][/color][/color]
[['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
'three']][color=blue][color=green][color=darkred]
>>> [x for x in y for y in beta][/color][/color][/color]
['C', 'C', 'C'][color=blue][color=green][color=darkred]
>>> [y for y in beta][/color][/color][/color]
[['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
'three']][color=blue][color=green][color=darkred]
>>> [x for x in y for y in beta][/color][/color][/color]
['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three']
Shoudn't both lines '[x for x in y for y in beta]' produce the same
list?
I'm guessing I'm the one confused here... but I'm confused! What's
going on?
Iain
up versions work (like this one from another thread: [i for i in
range(9) for j in range(i)]). I think I've figured that out, but I
found something strange along the way:
[color=blue][color=green][color=darkred]
>>> alpha = ["one", "two", "three"]
>>> beta = ["A", "B", "C"]
>>> [x for x in alpha for y in beta][/color][/color][/color]
['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three'][color=blue][color=green][color=darkred]
>>> [x for x in y for y in beta][/color][/color][/color]
['C', 'C', 'C'][color=blue][color=green][color=darkred]
>>> beta = [alpha, alpha, alpha]
>>> beta[/color][/color][/color]
[['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
'three']][color=blue][color=green][color=darkred]
>>> [x for x in y for y in beta][/color][/color][/color]
['C', 'C', 'C'][color=blue][color=green][color=darkred]
>>> [y for y in beta][/color][/color][/color]
[['one', 'two', 'three'], ['one', 'two', 'three'], ['one', 'two',
'three']][color=blue][color=green][color=darkred]
>>> [x for x in y for y in beta][/color][/color][/color]
['one', 'one', 'one', 'two', 'two', 'two', 'three', 'three', 'three']
Shoudn't both lines '[x for x in y for y in beta]' produce the same
list?
I'm guessing I'm the one confused here... but I'm confused! What's
going on?
Iain
Comment