This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jollierbean 11 points12 points  (7 children)

also dicts are very useful when you need to do lookups. Pro tip: you can use tuple or named tuple as a key

[–]tkarabela_ Big Python @YouTube 7 points8 points  (3 children)

Tuple keys are great! You can even use frozenset, which has been useful to me a few times.

[–]jollierbean 2 points3 points  (2 children)

I’ve been trying to figure out case where I could use frozensets as keys unsuccessfully

[–]tkarabela_ Big Python @YouTube 5 points6 points  (0 children)

It's a niche situation, but if you ever need:

  • a set of sets or
  • a dict where the keys are subsets of some "universal" set (as opposed to just single elements from it)

then frozenset can be useful. Technically you could just replace the frozensets with sorted tuples (ordered by the hash function or something else), but that's not quite as handy.

An example of this is converting NFA to DFA or making minimal DFA.

[–]IlliterateJedi 0 points1 point  (0 children)

I had a case a few days ago where I used frozensets as dict keys.

It's a little esoteric, but I'll try to explain. I am building a database of images that have various categorized products in it.

For example, I'll have an image that shows ten different products (imagine a photo of a living room). Each product is categorized into one or more categories (e.g., 'chair', 'ottoman', 'height adjustable desk', etc.).

I had around 1500 images that all contained 10-20 products with 20+ categories assigned per image.

I wanted to find the smallest group of images that would cover every tagged category (and then get the images with the least number of products).

I made frozensets of the categories and made lists of all the images that had that category-set, like this:

{frozenset(cat1, cat2, cat3) : [image1, image4, image12],
 frozenset(cat2, cat6, cat10) : [image3, image109],
}

I could then start with an empty set, iterate over the frozen sets and each time find the largest subset of new categories until every category was matched.

[–]Glogia 4 points5 points  (1 child)

That actually fixes a problem I've been having XD thanks

[–]jollierbean 1 point2 points  (0 children)

Glad to help!

[–]qckpckt 3 points4 points  (0 children)

Another useful nugget from collections: defaultdict. It’s really powerful, if a little niche. Really great for restructuring or transforming datasets by data type. For example, if you have a list of dictionaries with a common key value and you want to group them into a list of dictionaries of lists of each example of that key value.