0

I am a beginner to python (or coding in general!) and I am currently learning list comprehension. What I want to ask is if there exists a method to generate an array of floats between 0 and 1 using only list comprehension. The output should give the same result as :

for a in range(100):
    i = random()
    list.append(i)
print(list)

Any help would be appreciated :)

  • At first: list comprehensions create lists, not arrays (different type). Besides of that, show us what you have tried so far! – Klaus D. Apr 16 '21 at 06:19
  • 1
    On a second note, you are overwriting the builtin list function in your example, you should not do this. – MaxNoe Apr 16 '21 at 06:20

1 Answers1

1

You can use [random.random() for _ in range(100)]

This is an unusual case where you don't need a variable for the for. It's conventional to use _ to represent that.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118