Question Details

No question body available.

Tags

python

Answers (6)

March 29, 2026 Score: 3 Rep: 165,895 Quality: Medium Completeness: 70%

Yes, it's commonplace to use a list comprehension instead of a "for" loop like you show. More generally, if you'd ever write

l = []
for x in someiterator:
  l.append(f(x))

you can usually convert it to

l = [f(x) for x in someiterator]

This is shorter and apparently an imperceptibly tiny bit faster. If you're using an aggressive linter setup, the perflint W8401 warning or the equivalent Ruff PERF401 error will recomment using the list-comprehension syntax.

March 29, 2026 Score: 3 Rep: 149,444 Quality: Low Completeness: 40%

I don't understand what is the problem. But both code do the same. Some people prefers first version with normal for-loop, other prefer list comprehensions. Normal for-loops allows to add something more in loop - ie. print() for primitive debugging.

March 29, 2026 Score: 2 Rep: 37,006 Quality: Low Completeness: 40%

yeah, there's nothing "old fashioned" about OP's approach… not being aware of the second one just mean's the asker isn't very experienced at Python. List comprehensions have been around since Python 2.0, so October 2000, 25 years ago. They're fairly ubiquitous in code that has been written since then. There's nothing "newfangled" about [np.random.randint(min,max,) for _ in range(length)].

March 29, 2026 Score: 1 Rep: 29,242 Quality: Low Completeness: 20%

The second solution is using a list comprehension to achieve the same objective as your original solution. Both produce the same result.

List comprehensions such as the example shown here is concise and most Python programmers will understand what's going on at a glance.

However, some people take list comprehensions too far - i.e., they're very complex and hence difficult to read / maintain.

Your "old fashioned" approach is perfectly reasonable and arguably easier to understand if Python is not your main area of expertise.

March 29, 2026 Score: 0 Rep: 2,141 Quality: Low Completeness: 50%

You probably haven't encountered functional programming before. The following comprehensions may be of interest to you:

import random

generator

def generaterandomgen(minvalue, maxvalue, length): return (random.randint(minvalue, maxvalue) for in range(length)) gen = generaterandomgen(0, 3, 10) print([next(gen) for in range(5)], [next(gen) for in range(5)])

set comprehension

def generate
randomset(minvalue, maxvalue, length): return {random.randint(minvalue, maxvalue) for in range(length)} print(generaterandomset(0, 3, 100))

dict comprehension

def generaterandomdict(minvalue, maxvalue, length): return {i: random.randint(minvalue, maxvalue) for i in range(length)} print(generaterandomdict(0, 3, 10))

Output:

[3, 3, 0, 2, 2] [2, 1, 1, 1, 0] {0, 1, 2, 3} {0: 3, 1: 1, 2: 2, 3: 2, 4: 3, 5: 0, 6: 0, 7: 1, 8: 3, 9: 0}
March 29, 2026 Score: 0 Rep: 800 Quality: Low Completeness: 30%

One thing to note, is that list comprehensions tend to be faster than using for loops.