Masdfasd
Masdfasd

Reputation: 41

How to make a list using strings from another list?

So, in this code, I'm trying to make list_1 a list of 8 strings from alp_list where no strings are the same, and to delete the items in list_1 from alp_list for more codes. I'm not sure what's the best way to do this.

import random

alp_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
a = 0
list_1 = []
while a < 9:
    random_alp = random.choices(alp_list)
    list_1.append(random_alp)
    alp_list.remove(random_alp)
    a += 1
print (list_1)

Upvotes: 2

Views: 57

Answers (1)

Blue Owl
Blue Owl

Reputation: 131

You may use a preexisting function:

list_1 = random.sample(alp_list, 8)

To delete the items from the original list, you may go with:

for item in list_1:
    alp_list.remove(item)

Upvotes: 3

Related Questions