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.

PatDuJour
  • 893
  • 1
  • 10
  • 25
dvanaria
  • 6,593
  • 22
  • 62
  • 82

4 Answers4

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)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
30

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))
Joe Silverstein
  • 406
  • 5
  • 7
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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
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
mthecreator
  • 764
  • 1
  • 8
  • 19