1

This is the code:

word_list = ['cat','dog','rabbit']
letter_list = [ ]
fora_wordinword_list:`
   fora_letterina_word:
     letter_list.append(a_letter)
print(letter_list)

I will Modify the given code so that the final list only contains a single copy of each letter.

['c','a','t','d','o','g','r','b','i']

How can I do this?

RMPR
  • 3,368
  • 4
  • 19
  • 31
Might Mind
  • 29
  • 3
  • Does this answer your question? [Is there a function in python to split a word into a list?](https://stackoverflow.com/questions/113655/is-there-a-function-in-python-to-split-a-word-into-a-list) What you would need is to apply the function iterating the array. Does it help you? – Daemon Painter Apr 20 '20 at 08:54
  • OP doesn't want duplicates. – RMPR Apr 20 '20 at 09:05
  • [What should I do when someone answers my question](https://stackoverflow.com/help/someone-answers) – RMPR Apr 20 '20 at 09:14

7 Answers7

2

Python set is what you need. Try this:

word_list = ["cat", "dog", "rabbit"]
print(list(set("".join(word_list))))

Output:

['g', 'r', 'd', 'o', 'i', 'c', 't', 'b', 'a']

Warning:

sets DO NOT preserve the original order of the list.

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

However, if you are really interested in maintaining the order:

word_list = ["cat", "dog", "rabbit"]
result = []
for i in "".join(word_list):
    if i not in result:
        result.append(i)
print(result)

Output:

['c', 'a', 't', 'd', 'o', 'g', 'r', 'b', 'i']
abhiarora
  • 9,743
  • 5
  • 32
  • 57
2

Use chain.from_iterable from itertools:

list(dict.fromkeys(chain.from_iterable(word_list)))

This on python version 3.7+ will maintain order of elements also.

Austin
  • 25,759
  • 4
  • 25
  • 48
1

hope this will help

  word_list=["cat","dog","rabbit"]
   print(list(set("".join(word_list))))
soheshdoshi
  • 594
  • 3
  • 7
  • 24
1

Better to use a set then, to adjust your previous code to do just that:

word_list = ['cat','dog','rabbit']
letter_list = set()
for a_word in word_list:
    for a_letter in a_word:
        letter_list.add(a_letter)
print(list(letter_list))

You can also be clever with a one liner:

print(list(set("".join(word_list))))

And if you want to preserve order without importing anything just:

print(list({k: 1 for k in "".join(word_list)}))
RMPR
  • 3,368
  • 4
  • 19
  • 31
0
final_output = []
for word in word_list:
    for i in range( len(word) ):
        final_output.append(sampleStr[i])

Try this :)

Sharyar Vohra
  • 182
  • 1
  • 11
0

You have to add the condition:

word_list = ['cat','dog','rabbit']
letter_list = [ ]
for word in word_list:`
   for character in word:
     if character not in letter_list:
       letter_list.append(character)
print(letter_list)
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

In python set is not ordered. If you really care about the order, Then you can try something like this

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

for i in "".join(word_list):
    if i not in letter_list:
        letter_list.append(i)

>>> letter_list
['c','a','t','d','o','g','r','b','i']
mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26