Is it possible to going even further with a sort of triple parallel list comprehension

I love this simplification. I’m wondering if it’s possible to going even further with a sort of triple parallel list comprehension, like something along these lines (although this doesn’t actually work):

titles, poets, dates = [a, b, c for a, b, c in highlighted_poems_details]
1 Like

There is no way to derive three lists from one comprehension (that I can tell). The simplification above is about as simple as it gets.

1 Like

Good to know. Thanks!

1 Like

You can perform a trick where you effectively alter the order of the data with unpacking and zip-

titles, poets, dates = [list(elem) for elem in zip(*highlighted_poems_details)]

Have a look at how print(list(zip(*highlighted_poems_details))) compares to print(highlighted_poem_details) if you were curious about the way it works.

3 Likes

Yeah, that’s kind of what I thought but didn’t want to bring it up without testing, which I didn’t have time to do, earlier. Makes perfect sense to extract three intact lists from a zipped spread.

>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> e, f, g = [list(x) for x in zip(*a)]
>>> e
[1, 4, 7]
>>> f
[2, 5, 8]
>>> g
[3, 6, 9]
>>> 
2 Likes

Sure you can, but it takes away from the readability of the code, which is important. The first code snippet I can look at and tell in an instant what it does. It takes me a long time to unravel what
[list(elem) for elem in zip(*highlighted_poems_details)]
might be doing.
There isn’t a performance benefit to writing this way either.
That isn’t to say that writing it as a list comprehension would be wrong, it is a stylistic choice. The important part is to apply a clear, consistent and somewhat conventional style

4 Likes

You could exchange it for the following which is a touch more readable.
titles, poets, dates = map(list, zip(*highlighted_poems_details))
The transpose with zip trick is relatively well known so I don’t think it’s too troublesome but I’d agree with you the uses are more limited; any more complex than this and I’d avoid it. I would however choose to avoid .append and a for loop wherever possible.

>>> e, f, g = map(list, zip(*a))
>>> e
[1, 4, 7]
>>> f
[2, 5, 8]
>>> g
[3, 6, 9]
>>> 

There is a lot under the hood for such a simple overall statement. My biggest reservation is steering learners away from naive code before they are ready. One is continually harping about doing everything the naive way, first, and only refactor a step at a time.

#             unpack (splat) operator  list of lists
#                                   \  /
  e, f, g =    map  (  list,     zip(*a)  )
#  output   iterator  callback  iterable

Many learners are only too happy to ditch the clear naive code for what they imagine to be more elegant or Pythonic. Much of the time they don’t understand it, either, and yet still post it in the forums.

5 Likes

Thanks everyone for these fascinating suggestions! Slightly above my pay grade at the moment but I’ll do some research :slight_smile:

2 Likes

You’re welcome!

All in good time. Stick to the learning program and master the rudiments and naive coding approaches. They are the basis of everything. At or near the end of the track, circle back to stuff like this and put in the time, then. Happy coding!

2 Likes