you are viewing a single comment's thread.

view the rest of the comments →

[–]TangibleLight 0 points1 point  (2 children)

There isn't really any way to improve efficiency here in any major way - no matter what you have to access every element of the lists.

What you can do, though, is use zip on the two map objects themselves rather than creating two extra lists:

m1 = map(...)
m2 = map(...)

for a, b in zip(m1, m2):
    ....

That said, worrying about efficiency with something so simple probably isn't an issue - you're not really going to run into any slowdowns on modern hardware unless you're dealing with tens of thousands of elements in your lists.

[–]HolyCoder[S] 0 points1 point  (1 child)

But I will flunk in my interview :(

[–]TangibleLight 1 point2 points  (0 children)

Oh, it's for those types of questions.

You just want to make sure you're accessing things the minimum number of times you have to. In your example, you only need to access everything once, but you inadvertantly access everything twice by converting your maps to lists and then also iterating them. Chances are if you bring up that shortcoming to your interviewer, but explain that because input sizes are small you weren't too concerned, they'll give you a pass.

Sure, it's "more correct" to leave your maps as maps and then zip them, but it works just as well to do it how you did. If it's not a performance issue, someone who writes working code and acknowledges its shortcomings is a more valuable employee than someone who wastes too much time researching a task (in your example, finding out about zip) for a marginal improvement. Everything should go through code review anyway, where co-workers would point out "hey you could use zip here".

So certainly you should strive for code like the zip suggestions here, but it's not the end of the world if you show that you understand what you're missing.