Is there a more elegant way to write the following piece of Python?
[foo() for i in range(10)]
I want to accumulate the results of foo() in a list, but I don’t need the iterator i.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One way to do this is to use
_:This means exactly the same thing, but by convention the use of
_indicates to the reader that the index isn’t actually used for anything.Presumably
foo()returns something different every time you call it. If it doesn’t, and it returns the same thing each time, then you can:to replicate the result of calling
foo()once, 10 times into a list.