To remove Python deduplication

Asked 2 years ago, Updated 2 years ago, 24 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


© 2024 OneMinuteCode. All rights reserved.