To remove Python deduplication

Asked 1 weeks ago, Updated 1 weeks ago, 6 views

word_list = ["cat", "dog", "rabbit"]

letter_list = [ ]

for a_word in word_list:

    for a_letter in a_word:

        letter_list.append(a_letter)


print(letter_list)

How do I get the output value without duplication?

python

2022-09-19 23:22

1 Answers

word_list = ["cat", "dog", "rabbit"]
letter_set = set()

for a_word in word_list:
    for a_letter in a_word:
        letter_set.add(a_letter)

print(letter_set)

Why don't you try set?

word_list = ["cat", "dog", "rabbit"]
letter_list = []

for a_word in word_list:
    for a_letter in a_word:
        if a_letter not in letter_list:
            letter_list.append(a_letter)

print(letter_list)


2022-09-19 23:22

If you have any answers or tips


© 2022 pinfo. All rights reserved.