-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
  • 411
  • 1
  • 6
  • 16

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
  • 68,105
  • 3
  • 30
  • 44
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,980
  • 15
  • 25
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