Python Forum
For Loop with List Comprehension - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: For Loop with List Comprehension (/thread-31539.html)

Pages: 1 2 3


RE: For Loop with List Comprehension - bowlofred - Dec-17-2020

Depends on what you're looking for. There are many different ways to nest collections, and there are different ways to enumerate them. I don't know one single method that's appropriate for all. So I think it depends on the specifics and you may have to roll your own.


RE: For Loop with List Comprehension - muzikman - Dec-17-2020

No, it doesn't at all. I would never mix code like that together. I can't even understand it.


RE: For Loop with List Comprehension - muzikman - Dec-17-2020

I know. I found that out after playing with it.

(Dec-17-2020, 08:16 PM)buran Wrote: well, you don't need the list comprehension at all. it just creates the same list as the original.

world_cup_winners = [[2006, "Italy"], [2010, "Spain"],
                     [2014, "Germany"], [2018, "France"]]

for item in world_cup_winners:
    print(*item, sep=' ')
frankly, given that you know the sub-lists are 2-element in the format year, winner it may be more readable to write it like this

world_cup_winners = [[2006, "Italy"], [2010, "Spain"],
                     [2014, "Germany"], [2018, "France"]]

for year, winner in world_cup_winners:
    print(f'{year} {winner}')
I really hope the book does not suggest something like this, just to use list comprehension
world_cup_winners = [[2006, "Italy"], [2010, "Spain"],
                     [2014, "Germany"], [2018, "France"]]
[print(*item, sep=' ') for item in world_cup_winners]
It's really ugly and abuse of side effects using list comprehension.



RE: For Loop with List Comprehension - muzikman - Dec-18-2020

Basically, the product function eliminates multiple loops and lists on one line such as:

my_tuple = for n1 in list1 for n2 in list2
turns them into more understanding and less code lines:

[item for item in product(list1, list2)]
Something like that.

List Comprehensions just process the loops as you mentioned all in one line. Then you output the results.


RE: For Loop with List Comprehension - buran - Dec-18-2020

(Dec-17-2020, 11:46 PM)muzikman Wrote: I know. I found that out after playing with it.
It's a bit frustrating - you ask question then you reply this. Maybe don't rush to ask questions before you check the docs or experiment a bit.


RE: For Loop with List Comprehension - muzikman - Dec-18-2020

(Dec-18-2020, 06:06 AM)buran Wrote:
(Dec-17-2020, 11:46 PM)muzikman Wrote: I know. I found that out after playing with it.
It's a bit frustrating - you ask question then you reply this. Maybe don't rush to ask questions before you check the docs or experiment a bit.

Sounds like a good idea. Afterall, the best way to learn something is to find it on your own. I have all the resources I will ever need online. :)

Everyone says how easy Python is. It's not easy, especially coming from another core OOL. You could say it is somewhat unorthodox, compared to other languages. But, if you start with this language, then it wouldn't be at all.

Thanks again.