-1
import random
a=['sai','raju','phani'] 
b=[]
for I in a:
     b += random.Choice(a) 
print(b)

result:

['s', 'a', 'i', 's', 'a', 'i', 'r', 'a', 'j', 'u']

but expected to be total string not individual

['sai','sai','raju']

What did I do wrong?

raju
  • 1
  • 2

1 Answers1

-1

You can use random.choices and pass in the number of samples for b in the argument k.

import random
a=['sai','raju','phani']
b=[]

b = random.choices(a, k=len(a))

print(b)

EDIT: if you want it in a for loop:

for i in range(len(a)):
    b.append(random.choice(a))
tehCheat
  • 333
  • 2
  • 8