-1
import random
list1 = [11, 22, 33, 44, 55]
for i in range(5):
   random_number = random.randint(0,4)
   print(list1[random_number])

I am trying to find a way to print an entire list randomly without printing duplicates of the list in the first loop. Then if I increase the range to 10, 15, 20, and so on, every 5 prints displays all the elements randomly and without duplicates(by duplicates i mean no duplicates for every group of 5 prints displayed) i.e. 22, 33, 11, 55, 44, 44, 11, 33, 22, 55, 33, 22, 55, 44, 11 ...

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
muon012
  • 57
  • 5
  • 1
    Possible duplicate of [How can a print elements in a list randomly in python?](https://stackoverflow.com/questions/17880820/how-can-a-print-elements-in-a-list-randomly-in-python) – buran Dec 10 '18 at 21:44
  • 1
    Possible duplicate of [random iteration in Python](https://stackoverflow.com/questions/9252373/random-iteration-in-python) – Aurora Wang Dec 10 '18 at 21:45
  • this question does have the extra element of wanting to repeat the random iteration an arbitrary number of times, so just a partial duplicate, I think. – Steve Archer Dec 10 '18 at 22:39

1 Answers1

1

you could make a copy of the list, then pop elements out of the copy until it's empty, then 'reload' it by copying the original list again

import random
list1 = [11, 22, 33, 44, 55]
clonedList = list1.copy()
for i in range(5):
   if len(clonedList == 0):
       clonedList = list1.copy()
   random_number = random.randint(0,len(clonedList)-1)
   print(clonedList.pop(random_number))
Steve Archer
  • 641
  • 4
  • 10