Reputation: 992
For some reason my list doesn't get printed completely, but only word by word each time I run the script.
import random
passwords = ['test', 'password', 'heslo', 'lol']
var = random.choice(passwords).split()
for word in var:
print word
I wish the for loop
to print my whole list in randomized order each time I run my script. Upon removing random.choice
it works, but list is not randomized.
Upvotes: 0
Views: 660
Reputation: 45826
The issue is that choice
only selects a single random element from the list. I believe you meant to use shuffle
instead. shuffle
randomizes in-place the order of the list passed to it:
import random
passwords = ['test', 'password', 'heslo', 'lol']
# Randomize the order of the list
random.shuffle(passwords)
for word in passwords:
print word
This will alter passwords
. If you want the original list to remain unaltered, make a copy of it first:
pass_copy = passwords[:] # Slice copy
random.shuffle(pass_copy)
for word in pass_copy:
print word
Upvotes: 1