[Python] Locomotive Engineer: `fix_wagon_depot` and `zip()` are confusing

The Locomotive Engineer exercise is about packing and unpacking. The exercise, on the whole, is very nice. However, the fix_wagon_depot() requires transposing a 2D list. Most solutions use zip() and many students I’ve seen in the mentoring queue are quite confused about how to solve this and, when they look at community solutions, what zip() is and how this works.

Would it make sense to rethink/remove this last function?

CC: @Meatball @BethanyG

Yes, I have mentored a few people on the exercises and what I found as well is that some students have found the last function confusing. The point of the last function is to teach the student of functions in python where you have to “unpack”. I think there could make sense to rethink the last function.

So the introduction to the exercise has a clear example of zip(). But we could maybe provide more support there.

I don’t think removing the last task helps anything, but I do think we can explain zip() better.

I am tempted to interpret these symptoms as indicating that Hints don’t work. After all, the solution is right in there.


A worthwile goal. However, zip might not be ideal for this. It

  • deals with tuples, which are unfamiliar,
  • transforms data in an unfamiliar manner, and
  • is altogether unfamiliar itself (“why ‘zip’?”).

It might work better if an already well-known function were used instead. That way the student can more easily reuse prior knowledge.

I have no suggestions to offer (yet). I can only think of print, str.join, zip, and map right now – all not ideal. Oh, and min/max.

1 Like

All good points. But this exercise is quite a ways down the concept tree.

Its prerequisites include working with lists and tuples – and the associated practice exercises for some of those concepts have solutions that employ zip().

Unfortunately, a single encounter (or a few) does not make familiar. This is not critisism of the track.

It seems that the students expressing confusion about zip during mentoring sessions have somehow avoided it in prior exercises, or have forgotten about it, or failed to notice it. Whatever the case, these students are not familiar with zip.

I’m not sure that trying to plug the holes that these students slipped through is a viable approach. Or should I say: I expect it to be not as fruitful as one would like.

The exact solution is indeed in the hints file, but it does not explain how the zip works with the 2d list of tuples. Most explanations of how zip works use the same example [(1,2,3), (‘red’,‘green’,‘blue’)] or some such.

Perhaps a link to one of the tutorials that might go in a little more depth such as the RealPython page? Or another, better one where someone can look for more depth. Google searches mostly come up with the simple example.

I like that Real Python article. Excellent suggestion. I just wish RP wasn’t so aggressive with signup and paywalling. I’ll take a look and see if there are additional articles that might help. We might also call out zip() in a Exercism note in the instructions, and make sure that the example is one that maps better to the task.

Ultimately, Matthijs and Isaac do have a point - having a concept article and/or specific concept exercise is probably the ideal solution (maybe in conjunction with some other built-in iterator-related functions) – or we include zip() as part of an iterators concept exercise. However, that’s probably not going to happen right away.

I know a band-aid is not ideal, but I also think its better than nothing. Unless we can replace the zip() with something else in that last task?

Please remove this task , i am stuck in it and i dont find it useful nor did it teach me anything but not to use zip ever again

If you’re stuck on it, would you like help? If so, please share your code. zip() is a very useful function to have in your toolkit!

*lis,=zip(*wagons_rows)
return lis
it returns list[tuple[tuple]] instead of list list tuple

and on vscode it returns tuple list tuple , i am very frustrated and confused

i give up i need your help please hahaha

Those tuples need to be lists. You can use codeblocks to make code readable.

You can use list() to convert things to lists.

i tried list() but it still doesn’t work , it gives the same result

What exactly did you try? Can you share your code in a codeblock? You need to call list on all the generated elements.

here:

def fix_wagon_depot(wagons_rows):

    rest=zip(*wagons_rows)

    return list(rest)

i tried for loops , nested for loops and tried the zip() function with other parameters but this is closest to the answer because as it said in the hints in transposes the list , i have been at this for hours im just super confused i dont know what to do

zip generates an iterable of tuples. Each of those tuples need to be converted to a list. You can print what that returns to see the data and what needs fixing.

1 Like

ok i finally did it :

    rec=[]
    *lis,=zip(*wagons_rows)
    for it in lis:
        it=list(it)
        rec.append(it)
    return rec

i don’t know how to feel

thank you

Well solved! You could request mentoring to further improve that, too!