Week 5 Exercise 1 Q5

My solution:

Input = {'a':1, 'b':2, 'c':1, 'd':1, 'e':2, 'f':3, 'g':4, 'h':3, 'i':4}
Output = []
for item in Input:
    if Input[item] in Output:
        pass
    else:
        Output.append(Input[item])  
print(Output)   

Any comments?

2 Likes

Have you tried using set ?

Thanks. With set, the coding has become more cryptic with add() command with the set having unique values.

Input = {'a':1, 'b':2, 'c':1, 'd':1, 'e':2, 'f':3, 'g':4, 'h':3, 'i':4}
Output = set()
for item in Input:
    Output.add(Input[item])  
print(Output)  
{1, 2, 3, 4}
1 Like

bit more tidy code:

    Input = {'a':1, 'b':2, 'c':1, 'd':1, 'e':2, 'f':3, 'g':4, 'h':3, 'i':4}
    Output = {val for val in Input.values()}
    print(Output)
1 Like

Thanks, Amazing versatility of Python.

1 Like

def unique_val(x):
l=[ ]
for item in x:
l.append(x[item])
s=list(set(l))
return s