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 - muzikman - Dec-17-2020

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?


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

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.


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

That is really cool. Fantastic.


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

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



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

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 - bowlofred - Dec-17-2020

(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')]



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

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.


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

(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


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

I apologize for all the questions. I do appreciate for all of the help.


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

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')]