Chalarangelo / 30-seconds-of-python

Short Python code snippets for all your development needs
https://www.30secondsofcode.org/python/p/1
Creative Commons Attribution 4.0 International
8.83k stars 1.26k forks source link

[BUG] non-keyable values in collect_dictionary snippet #224

Closed azanbinzahid closed 4 years ago

azanbinzahid commented 4 years ago

collect_dictionary snippet it works fine for given example with a key value pair in which a value can be converted into a key. What if the values are of types which cannot be converted to key like a list or obj? The current snippet throws an error for this:

ages = {
    "a": 1,
    "b": [1, 2, 4],
}

It is because a list cannot be converted into a key. I have two solutions:

  1. Edit the same snippet and cater for values of type list and make each list entry a unique key and it's original key as value.
  2. Make a new snippet collect_list_dictionary and cater for values with type list
Trinityyi commented 4 years ago

The snippet clearly states that it only works for hashable values. Therefore this behavior is by design. Closing as wontfix.

azanbinzahid commented 4 years ago

Thank you for looking at it. Can I submit a new snippet for non-hashable value?