0

I would like to generate a list of random numbers and letters.

I managed to make this:

def rand_num():
  
    while True:
        random_char= random.choice(string.ascii_letters+string.digits)
    
        random_lst= [random_char]

but when I want to print this random_lst I get an output like this:

['W']
['i']
['P']
['6']
['P']
['B']
['d']
['f']
['n']
['j']

instead of:

['W','i','P'.... and so on

What should I do? Is .choice the wrong function in this case?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
user1509923
  • 193
  • 1
  • 3
  • 13

2 Answers2

5

Try this for a list of length 10:

random_list = [random.choice(string.ascii_letters+string.digits) for n in xrange(10)]

Or this for a string of length 10:

random_string = ''.join(random.choice(string.ascii_letters+string.digits) for n in xrange(10))
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
4

You should have this instead:

random_lst.append(random_char)
squiguy
  • 32,370
  • 6
  • 56
  • 63