Reputation: 1255
I am a beginner in Python and I have a following question regarding a generator. My generator yields three variables:
def generate():
for i in range(1, 10):
j = i + 1
k = i ** 2
yield i, j, k
In the following function, I would like to loop over the variable j
only. Of course, this works:
for var_i, var_j, var_k in generate():
print("This is my varible j: ", var_j)
But I have two unused variables here - var_i, var_k
. So I would like to ask, if there is a better way, how to do this?
This answer did not help me: Looping through a generator that returns multiple values Thanks a lot.
Upvotes: 0
Views: 192
Reputation: 782488
The convention is to use _
as a variable when you're not interested in using the value.
for _, var_j, _ in generate():
Upvotes: 5