-3

I have a list here

List = ['first', 'second', 'third', 'fourth', 'etc']

How can I loop through List randomly with random indices and print each item in the random index. For example I want the output to look something like this:

Output:

fourth
first
etc
third
second
an4s911
  • 401
  • 6
  • 15

3 Answers3

2

If you want to do this without mutating the original list, you can use random.sample:

>>> import random
>>>
>>> items = ['first', 'second', 'third', 'fourth', 'etc']
>>> print("\n".join(random.sample(items, len(items))))
fourth
second
third
etc
first

Also, get in the habit of giving your variables lowercase names that don't conflict with built-in functions or types! :)

Samwise
  • 51,883
  • 3
  • 26
  • 36
0

You can randomize the list like this:

import random

List = ['first', 'second', 'third', 'fourth', 'etc']
for i in random.shuffle(List):
    print(i)
Keldorn
  • 1,818
  • 12
  • 22
0

you can use random.shuffle

import random
List = ['first', 'second', 'third', 'fourth', 'etc']
random.shuffle(List)
print(List)

hope that's what you were looking for