48

I'm struggling with a minor problem in Python (my program is in version 3.2.3 right now).

I have a dictionary that looks like this (this is just an example, actually taken from another post here):

[
    {"abc": "movies"},
    {"abc": "sports"},
    {"abc": "music"},
    {"xyz": "music"},
    {"pqr": "music"},
    {"pqr": "movies"},
    {"pqr": "sports"},
    {"pqr": "news"},
    {"pqr": "sports"},
]

I want to simply print() a list of unique values, eliminating duplicates. At the end of this list, I would like to print the number of unique values in the dictionary:

movies
sports
music
news

4

Any help is appreciated. There are some other posts here I found that were somewhat related, but I don't know Python well enough to apply it to this specific problem.

1
  • 14
    you have a list of dictionaries.
    – jfs
    Jun 20, 2013 at 15:53

4 Answers 4

62

Use set here as they only contain unique items.

>>> lis = [{"abc":"movies"}, {"abc": "sports"}, {"abc": "music"}, {"xyz": "music"}, {"pqr":"music"}, {"pqr":"movies"},{"pqr":"sports"}, {"pqr":"news"}, {"pqr":"sports"}]
>>> s = set( val for dic in lis for val in dic.values())
>>> s 
set(['movies', 'news', 'music', 'sports'])

Loop over this set to get the expected output:

for x in s:                                
    print x
print len(s)   #print the length of set after the for-loop
... 
movies
news
music
sports
4

This line s = set( val for dic in lis for val in dic.values()) is roughly equivalent to:

s = set()
for dic in lis:
   for val in dic.values():
      s.add(val)
2
  • 6
    Points for explaining the nested list comprehension. Nov 20, 2013 at 21:38
  • 3
    That nested list is crazy, would have never thought of that. Aug 27, 2015 at 3:48
31

You can exploit the properties of set for this purpose; each element is unique and duplicates will be ignored.

uniqueValues = set(myDict.values())

By using set in this way, duplicate elements returned from .values() will be discarded. You can then print them as follows:

for value in uniqueValues:  
    print(value)  
print(len(uniqueValues))
1
  • This doesn't work if the values of myDict are dictionaries as well
    – Castor
    Dec 29, 2021 at 0:32
12

Use a set():

from itertools import chain

unique_values = set(chain.from_iterable(d.values() for d in dictionaries_list))
for value in unique_values:
    print(value)

print(len(unique_values))

Use .itervalues() on Python 2 for a little more efficiency. Because you have a list of dictionaries, we need to pull out the values from each of those dictionaries first; I used itertools.chain.from_iterable() with a generator expression to list all values in a sequence:

>>> for value in set(chain.from_iterable(d.values() for d in dictionaries_list)):
...     print(value)
... 
news
sports
movies
music

The alternative is to use nested loops in the generator expression:

>>> for value in set(v for d in dictionaries_list for v in d.values()):
...     print(value)
... 
news
sports
movies
music
7
dict = {'511':'Vishnu','512':'Vishnu','513':'Ram','514':'Ram','515':'sita'}
list =[] # create empty list
for val in dict.values(): 
  if val in list: 
    continue 
  else:
    list.append(val)

print list
1
  • A set would be more efficient for this. Using the in operator is not as efficient for lists. Apr 9, 2019 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.