Reputation: 141
I'm a beginner, so please bear with me
I have the following code:
shapes = ['rect', 'rect', 'circle']
coords = ["34,44,210", "290,333,250", "337,300,44"]
for shape in shapes:
for coord in coords:
print(shape)
print(coords)
I am very well aware that this prints it out multiple times, but I'm not able to find a way to avoid it.
A better way I would think, is to use it one-by-one inside the print statement, something like this:
print(shape for shape in shapes)
print(coord for coord in coords)
But this doesn't work, and only prints
<generator object <genexpr> at 0x00000282B2C69510>
<generator object <genexpr> at 0x00000282B2C69510>
I'm not able to find a guide on printing the for loop inside the print statement.
I'm using this to be able to print something like:
id='rect' id2='34,44,210'
id='rect' id2='290,333,250'
id='circle' id2='337,300,44'
Is there a way? Thanks in advance.
Edit: I want to be able to print that exact thing.
Upvotes: 0
Views: 117
Reputation: 52089
Using a generator expression is already going in the right direction. However, it must be unpacked in the call to get the items out of the generator into the print
:
print(*(shape for shape in shapes))
This is a bit useless in this case, since the generator does not do anything useful – print(*shapes)
works as well here.
As a more useful case, if you want to print all combinations of shapes and coords, a generator over both sequences can be used:
shapes = ['rect', 'rect', 'circle']
coords = ["34,44,210", "290,333,250", "337,300,44"]
print(*((shape, coord) for shape in shapes for coord in coords))
Python ships with various utilities that already implement common iteration patterns. For example, the combination of the two lists can be created via itertools.product(shapes, coords)
.
What you likely need for such data is the pairwise folding of the two iterables. This is a common use-case and thus Python has a helper which is always available: the builtin function zip
.
>>> shapes = ['rect', 'rect', 'circle']
>>> coords = ["34,44,210", "290,333,250", "337,300,44"]
>>> # unpack pairs directly
>>> print(*zip(shapes, coords))
('rect', '34,44,210') ('rect', '290,333,250') ('circle', '337,300,44')
>>> # v ---- format each pair ---- v
>>> print(*(f"{shape}={coord}" for shape, coord in zip(shapes, coords)))
rect=34,44,210 rect=290,333,250 circle=337,300,44
>>> # v ---- format each pair ---- v v separate by newline
>>> print(*(f"{shape}={coord}" for shape, coord in zip(shapes, coords)), sep="\n")
rect=34,44,210
rect=290,333,250
circle=337,300,44
Keep in mind though that there is nothing wrong about using regular for
loops. If the choice is between a single newline-separated printing unpacked formatted zipped pairs expression and several statements, use the more readable option.
for shape, coord in zip(shapes, coords):
print(shape, '=', coord, sep='')
Upvotes: 3
Reputation:
The syntax you used here:
print(shape for shape in shapes)
is legal but it's a generator comprehension which is similar to list comp but returns a generator object.
Howerver, since your desired outputs are strings, you can join them with the newline escape pair \n
and print the whole string once:
print("\n".join((i for i in shapes)))
Notice that the str casting is redundant since the element are strings already.
Upvotes: 0
Reputation: 82
print('The shapes are:', '\n'.join([shape for shape in shapes]))
The lists are: 'rect'
'rect'
'circle'
Upvotes: 0