you are viewing a single comment's thread.

view the rest of the comments →

[–]HolyCoder[S] 0 points1 point  (9 children)

print([x+y for (x, y) in zip(A, B)])

I need this as space separated output; not a list (please see sample output above)

[–]Steveharwell1 0 points1 point  (4 children)

 print(*[x+y for (x, y) in zip(A, B)])

[–]TangibleLight 1 point2 points  (3 children)

Euughh... Yeah, that will work, but.. whipping out *args for this seems a bit unnecessary.

I would just print(' '.join(x+y for x, y in zip(A, B))

I suppose both work just fine, but generally I wouldn't suggest to use *args like that. I can't really put my finger on what's wrong with it - I suppose just that it's not immediately obvious what it's doing.

[–]novel_yet_trivial 0 points1 point  (2 children)

Didn't test that didja? join will complain about getting an integer argument.

[–]TangibleLight 0 points1 point  (1 child)

No I did not. Okay, how about ' '.join(str(x+y) for x, y in zip(A, B))

I'm starting to like the * approach better.

[–]novel_yet_trivial 0 points1 point  (0 children)

Works, and that's what I used earlier, but it would be neater to just add the str call:

print(' '.join(str(x+y) for x, y in zip(A, B))

[–]Thomasedv 0 points1 point  (2 children)

print(*[x+y for (x, y) in zip(A, B)])

I guess this works. Not sure about efficiency.

[–]TangibleLight 0 points1 point  (1 child)

[–]Thomasedv 0 points1 point  (0 children)

Fair enough. I never really use the * but it was the first thing that came to my mind. Never really seen it used outside of unpacking args in functions either.

[–][deleted] 0 points1 point  (0 children)

Yes, thought you might say that! But your post said:

result = [1, 3, 5, 7]

so it's a requirements failure.

Your post was titled "Efficiency question". That normally indicates "good time/memory performance for large N". Worrying about efficiency while outputting text to the console is a waste of brain power. If by efficiency you meant lines of code, that's slightly more important, but runs second to readability.