Andrew
Andrew

Reputation: 409

Count unique values in Python list

I have the following code I've been working on, but I can't seem to find a way to count the number of unique values in the anagram list. If I just print out: print len(anagram) I get the total value of the list, but it includes duplicates.

I have tried to convert the list to a set and back to get rid of the duplicates, but have not had any luck.

Thanks!

#import libraries 
import urllib, itertools

#import dictionary
scrabble = urllib.urlopen('http://www.puzzlers.org/pub/wordlists/ospd.txt').read().split()
#print the length of the dictionary
print len(scrabble)

#make anagrams from scrabble list
anagrams = [list(g) for k,g in itertools.groupby(sorted(scrabble, key=sorted), key=sorted)]


#print the largest anagram  
print "Largest number of anagrams for a word : ", len(max(anagrams, key=len)) 
print "Largest anagram word and values ", max(anagrams, key=len)

Upvotes: 0

Views: 5349

Answers (1)

nneonneo
nneonneo

Reputation: 179717

Use a set. sets hold only unique values:

print(len(set(anagram)))

Upvotes: 2

Related Questions