Building a randomized list in Python

Viewed 65

I have a bit of an issue with my code here. I'm trying to build a program that generates a new workout routine every day. It should pull from the same list of workouts and create a new list of what the user will use that day. When I run the program, it returns a list of the same item repeated 4 times, when it should be a list of 4 unique items. I am quite new to Python and programming, but I can't seem to find the solution anywhere. I would greatly appreciate any help or advice. How can I get 4 random items from a list, append them to a new list, and have none repeating?

import random

chest_day_heavy = [["benchpress"], ["incline benchpress"], ["DB benchpress"], ["inclince DB press"],["seated machine press"]]
def workout_generator(muscle_group):
  workout_otd = []
  random_generator = random.randint(0,len(muscle_group))
  while len(workout_otd) < 4:
    workout_otd.append(muscle_group[random_generator])
    for i in range(len(workout_otd)):
      if muscle_group[random_generator] == workout_otd[i]:
        continue
  return workout_otd

print(workout_generator(chest_day_heavy))

The result, for example, I would like to be something like: [["inclince DB press"],["seated machine press"],["benchpress"],["DB benchpress"]]. No item repeated. Instead, I get something like this: [["seated machine press"],["seated machine press"],["seated machine press"],["seated machine press"]]

I may have bitten off more than I can chew after only a month of self-teaching haha

5 Answers
random_generator = random.randint(0,len(muscle_group))

Should be inside the while:

....
while len(workout_otd) < 4:
    random_generator = random.randint(0,len(muscle_group))
    workout_otd.append(muscle_group[random_generator])
    ....

As Gabriele said .choise() is a good way of doing that, or you can use

random.shuffle(lst)

and then print first 4 items.

the issue with OP's random.randint usage is that it pins down to only 1 index (thus repeating the same single item).

random module comes with a few excellent functions, such as shuffle() which can be used like:

def workout_generator(muscle_group):
    r = [*muscle_group]  # shallow copy the input list
    random.shuffle(r)
    return r

That is probably the easiest method:

import random

chest_day_heavy = [["benchpress"], ["incline benchpress"], ["DB benchpress"], ["inclince DB press"],["seated machine press"]]
workout_otd=[]
def workout_generator(workout_otd):
    workout_otd=chest_day_heavy.copy()
    random.shuffle(workout_otd)
    return workout_otd

workout_otd= workout_generator(workout_otd)
print(workout_otd)

with the function random.shuffle() it shuffles all items

To print only the first 4 elements you can do it like this: print(workout_otd[0:4])

You can reduce your code greatly using other functionality of the random module,

import random

chest_day_heavy = [["benchpress"], ["incline benchpress"],
                        ["DB benchpress"], ["inclince DB press"],
                        ["seated machine press"]]

def workout_generator(muscle_group, n_wk):
    random.shuffle(muscle_group)
    return muscle_group[:min(n_wk,len(muscle_group))]

print(workout_generator(chest_day_heavy, 4))

Now the list is randomly rearranged in place, and the function returns first N elements out of it, as requested. For flexibility, the number of elements - workout routines - are part of the function argument list.

Related