19335

python Get the unique values from a dictionary

Question:

I want to get the unique values from my dictionary.

Input:

{320: [167], 316: [0], 319: [167], 401: [167], 319: [168], 380: [167], 265: [166]}

Desired Output:

[167,0,168,166]

My code :

unique_values = sorted(set(pff_dict.itervalues()))

But I'm getting this error : TypeError: unhashable type: 'list'

Answer1:

It is not clear why you are mapping to single-item lists as values, but you can use a list comprehension to extract the elements.

foobar = {320: [167], 316: [0], 319: [167], 401: [167], 319: [168], 380: [167], 265: [166]} print list(set([x[0] for x in foobar.values()]))

If you start out by mapping directly to values though, the code can be much simpler.

foobar = {320: 167, 316: 0, 319: 167, 401: 167, 319: 168, 380: 167, 265: 166} print list(set(foobar.values()))

Answer2:

Lists do not qualify as candidate set content because they are <em>unhashable</em>.

You can merge the items into one container using <a href="https://docs.python.org/3/library/itertools.html#itertools.chain.from_iterable" rel="nofollow">itertoos.chain.from_iterable</a> before calling set:

from itertools import chain unique_values = sorted(set(chain.from_iterable(pff_dict.itervalues())))

Note that using itertools does not violate your choice of dict.itervalues over dict.values as the unwrapping/chaining is done <em>lazily</em>.

Answer3:

set expect an elements not list of list, So you have to create a list of elements using list comprehension

In [34]: sorted(set([i[0] for i in d.values()])) Out[34]: [0, 166, 167, 168]

Recommend

  • Set confirmation prompt box's title
  • How to generate key hash for Facebook?
  • Should authentication be in a separate service for wcf?
  • Determine repeat characters in a php string
  • wxpython notebook inside boxsizer
  • Google maps error during visualization
  • How do I click on a Cell of a DataGridView programmatically?
  • summarize data from csv using R
  • python Matplotlib candlestick plot works only on daily data, not for intraday
  • get value from xml with r by attribute
  • Return IIS Windows Authentication Provider in PowerShell for Desired State Configuration script
  • Is there a way to remove a script from a doc (using the new doc embedded script)
  • How to assert page/tab/window title in Behat + Mink
  • Background beacon detection and Notification (Both iOS and Android) for Eddystone beacon?
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • plot dirac function in matlab
  • yii rewrite url with many sub categories
  • How do I set the logging properties in a spring java configuration?
  • I need a PHP mySQL Login script that directs user to specific URL based on username and password
  • Modifying native query cannot have named parameter bindings?
  • Use AutoIt with java applications
  • separate tokens in batch file
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Magento-Change Attribute of All Products
  • Set SelectedIndex of ListView in FlipView_SelectionChanged event
  • Find angle of point on circle
  • Rotating Towards Path in OpenGL
  • How to use Streams api peek() function and make it work?
  • How to change user identity when git pushing via ssh?
  • How to decleare char *const argv[] in swift [duplicate]
  • How to encrypt Connectionstring written in web.config from codebehind?
  • How to get rgb from transparent pixel in js
  • Can someone explain this Java code (formatting the output using System.out.format) to me?
  • WPF custom control and direct content support
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • XSLT Transformation to validate rules in XML document