Python Forum
For Loop with List Comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop with List Comprehension
#21
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.
Reply
#22
No, it doesn't at all. I would never mix code like that together. I can't even understand it.
Reply
#23
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.
Reply
#24
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.
Reply
#25
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#26
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,680 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,201 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,176 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  Using recursion instead of for loops / list comprehension Drone4four 4 3,073 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Appending to list of list in For loop nico_mnbl 2 2,329 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020