-1

What I'm getting and what I need to get pic

text = "the faith that he had had had had an affect on his life"

dictionary = {}
text = text.split()
for word in text:
    key = len(word)
    if key in dictionary.keys():
        dictionary[key] += [word]
    else:
        dictionary[key] = [word]
return dictionary

For the word in the text, there are repeated words. How do I return the dictionary with no repeated words?

martineau
  • 119,623
  • 25
  • 170
  • 301
J_lll
  • 55
  • 1
  • 9

2 Answers2

0
List=[]
c=1
For n in list:
        If(n==list[c]):
             list.pop(c)
        C+=1
Udantha
  • 77
  • 1
  • 4
  • 12
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Feb 12 '17 at 10:53
-3

You can use set(). Sets are basically lists that cannot have duplicated values. So, you convert your list to a set (it removes the duplicated values), and convert it back to a list

my_list = list('hello world')

def remove_duplicate(ls):
    return list(set(ls))

print(remove_duplicate(my_list))

The problem is that it doesn't keep the order of the list's items. (at least on python 3.4). So, you can make your own function:

my_list = list('hello world')

def remove_duplicate(ls):
    new = []
    for item in ls:
        if item not in new:
            new.append(item)
    return new

print(remove_duplicate(my_list))
math2001
  • 4,167
  • 24
  • 35