lavish
lavish

Reputation: 43

Generate a list of characters

I am trying to create a list of characters using the following code:

import random
import string
def random_char():
       return ','.join(random.sample(b,26))

a=[]
b=[]
b=string.ascii_uppercase
a=[random_char()]
print(a)
print(a[0])

Output:

['P,K,R,E,J,W,X,B,C,G,N,Z,L,S,H,Q,D,I,M,U,O,A,Y,F,V,T']
P,K,R,E,J,W,X,B,C,G,N,Z,L,S,H,Q,D,I,M,U,O,A,Y,F,V,T

When i try to print a[0],the entire list gets printed.How do i separate the characters as separate elements of the list?

Upvotes: 1

Views: 367

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

','.join(random.sample(b,26)) creates a single string. So your list has a single element which is that string.

If you want a list just return random.sample(b,26) and assign a to the output:

def random_char():
       return random.sample(b,26)
a = random_char() # now a list of separate elements
print(a)
['S', 'I', 'P', 'O', 'L', 'Y', 'J', 'T', 'C', 'F', 'Z', 'A', 'B', 'M', 'V', 'D', 'K', 'H', 'X', 'N', 'Q', 'U', 'G', 'W', 'E', 'R']

print(a[0])
S

Upvotes: 2

Related Questions