Python Forum
For Loop with List Comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop with List Comprehension
#11
I tried it without your print statement and it didn't come out formatted.

print(*item, sep=' ')
works fine.

print(item, sep=' ')
doesn't remove the raw output formatting.

Why is this?
Reply
#12
print(item, sep=' ') - you pass just one object as first argument to print function, a tuple with n elements. It prints a tuple (i.e. incl brackets and elements separated by comma - that is str representation of a tuple). In this case sep does not come into play - there is one argument.

print(*item, sep=' ') - note the asterisks. This is iterable unpacking. you unpack the iterable (i.e. the tuple). print function gets n positional elements, not single tuple. Then sep comes into play and you get the output you expect.
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
#13
That is really cool. Fantastic.
Reply
#14
Am I partially correct when I say, there is no need for more than 2 nested loops wen you have list comprehensions? Here is another problem in the book, I solved with the same format.

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

my_tuple = [item for item in world_cup_winners]
for item in my_tuple:
    print(*item, sep=' ')
Output:
2006 Italy 2010 Spain 2014 Germany 2018 France
Reply
#15
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.
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
#16
(Dec-17-2020, 07:02 PM)muzikman Wrote: I adjusted it according to "bowlofred" without the "product" function. What does this function do anyway?

You can always look it up at the official documentation site.

Product takes some number of iterables (your lists) and makes every combination of one of each of the iterables. Example:
>>> from itertools import product
>>> list(product([1,2,3], [10,20,30]))
[(1, 10), (1, 20), (1, 30), (2, 10), (2, 20), (2, 30), (3, 10), (3, 20), (3, 30)]
You could do the same with some for loops, but product handles arbitrary numbers of them without having to change the code (like going from 2 columns to 3 columns).

>>> list(product([1,2,3], [10,20,30], ["A", "B", "C"]))
[(1, 10, 'A'), (1, 10, 'B'), (1, 10, 'C'), (1, 20, 'A'), (1, 20, 'B'), (1, 20, 'C'), (1, 30, 'A'), (1, 30, 'B'), (1, 30, 'C'), (2, 10, 'A'), (2, 10, 'B'), (2, 10, 'C'), (2, 20, 'A'), (2, 20, 'B'), (2, 20, 'C'), (2, 30, 'A'), (2, 30, 'B'), (2, 30, 'C'), (3, 10, 'A'), (3, 10, 'B'), (3, 10, 'C'), (3, 20, 'A'), (3, 20, 'B'), (3, 20, 'C'), (3, 30, 'A'), (3, 30, 'B'), (3, 30, 'C')]
Reply
#17
Quote:You can always look it up at the official documentation site

That's crazy talk. Far better to ask an endless string of questions and walk away with an incorrect interpretation of what was said.
Reply
#18
(Dec-17-2020, 09:03 PM)deanhystad Wrote: That's crazy talk. Far better to ask an endless string of questions and walk away with an incorrect interpretation of what was said.
Smile
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
#19
I apologize for all the questions. I do appreciate for all of the help.
Reply
#20
I get it. How about looping through nested collections?

(Dec-17-2020, 08:33 PM)bowlofred Wrote:
(Dec-17-2020, 07:02 PM)muzikman Wrote: I adjusted it according to "bowlofred" without the "product" function. What does this function do anyway?

You can always look it up at the official documentation site.

Product takes some number of iterables (your lists) and makes every combination of one of each of the iterables. Example:
>>> from itertools import product
>>> list(product([1,2,3], [10,20,30]))
[(1, 10), (1, 20), (1, 30), (2, 10), (2, 20), (2, 30), (3, 10), (3, 20), (3, 30)]
You could do the same with some for loops, but product handles arbitrary numbers of them without having to change the code (like going from 2 columns to 3 columns).

>>> list(product([1,2,3], [10,20,30], ["A", "B", "C"]))
[(1, 10, 'A'), (1, 10, 'B'), (1, 10, 'C'), (1, 20, 'A'), (1, 20, 'B'), (1, 20, 'C'), (1, 30, 'A'), (1, 30, 'B'), (1, 30, 'C'), (2, 10, 'A'), (2, 10, 'B'), (2, 10, 'C'), (2, 20, 'A'), (2, 20, 'B'), (2, 20, 'C'), (2, 30, 'A'), (2, 30, 'B'), (2, 30, 'C'), (3, 10, 'A'), (3, 10, 'B'), (3, 10, 'C'), (3, 20, 'A'), (3, 20, 'B'), (3, 20, 'C'), (3, 30, 'A'), (3, 30, 'B'), (3, 30, 'C')]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 542 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 484 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,261 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,426 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,736 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,861 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,253 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,228 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  Using recursion instead of for loops / list comprehension Drone4four 4 3,155 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Appending to list of list in For loop nico_mnbl 2 2,373 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