Unpack a Tuple and append it to multiple lists

Hello, is it possible to unpack a tuple and then append it to multiple lists?

This question may be relevant to this exercise: https://www.codecademy.com/courses/learn-python-3/lessons/string-methods/exercises/review-ii

I have shortened the code straight to the point relevant to my question: dpaste: 595TQGNQ6

I have created a tuple that contains three empty lists with each list referencing the variables titles, poets, and dates in order. So instead of writing append() three times, I’m wondering is there a way to put that into a one-liner.

I feel I should probably prepend this with a note that some people really don’t like the unpacking operator and especially combining it with zip (think readability and long term maintenance). Therefore you should think twice about writing code this way. Can it be understood? Can it be changed easily?

Writing single use code is very different to code that others have to read and work with. One liners in general might save you several seconds typing but they also risk slowing down long term development. So just consider carefully whether or not shorter code is better code here (and in general). Wiser heads than mine would urge you to consider long term maintenance above all else, certainly more so than saving yourself a few seconds now.

Python has an unpacking operator * which may be close to what you want-


One minor issue is that your original data is packed the wrong way for this, but there’s a near trick to combine both zip and the unpacking operator effectively transposing your data. Compare the two-


If you can’t read that easily then try the following (more for debugging than actual useful code)-

print(*highlighted_poems_details, sep="\n", end="\n\n")
print(*zip(*highlighted_poems_details), sep="\n")

The important bit there is the zip(*iterable) part.

End result

titles, poets, dates = zip(*highlighted_poems_details)

If this was combined with a list comprehension or mapping you could get lists from iterating through the zip instead of tuples. Once again though, readability and especially long term maintenance should always be considered (a for loop is easy to change bit by bit, a comprehension is not).

1 Like

Regarding readability, if the length of a one-liner (especially when using a listcomp) isn’t long, I don’t see the downside of it unless it contains some very complex code and may therefore be incomprehensible to some.

I only ever write one-liners if it is not difficult to decipher it in one glance by other people (yes, most of the times I’d post a part of my code in different programming communities for others to review it and give feedback about it).

About your end result, is there a difference between appending it individually and writing it in this way titles, poets, dates = map(list, zip(*poem_details)), the results of both are the same and minus the use of for loop for the latter. If maintenance is the main concern here, wouldn’t developers waste more time to maintain code that uses while and for loop than just maintaining that one-liner?

I might be the wrong person to ask; I quite like unpacking. It could become a little trickier when you put it together as a function; perhaps an additional piece of data can be processed. But, you’ve got a function that only deals with three values, so you’re forced to add two separate functions for basically the same task (properly re-writing the function would break essential code). Altering a for loop with an if statement to simply append an extra list if four values are passed instead of three is quite straightforward. When a depends on b depends on c etc. etc. having scalable code counts for a lot. Avoiding hard coding can arguably go for variables as much as expressions.

There’s no strict rule but you might find whoever you’re working with has a convention they stick to, probably for a good reason. It’s worth knowing unpacking either way, both in case someone else uses it and because sometimes it might well be a better solution. I only raised this a point because you may well run into those who dislike this style.