Search code examples
pythonloopsdictionarysetfrozenset

Retrieve values from a frozen set dictionary (python)


I have a frozen set dictionary of the form:

{frozenset({12345, 3245}): 45.95948791503906,
 frozenset({12345, 12804138}): 48.996036529541016,
 frozenset({3245, 9876}): 50.67853927612305,

Is it possible for me to iterate over the values based on one of the key from the frozenset?

Example:

If I provide the value 12345, I want to return

frozenset({12345, 3245}): 45.95948791503906,
frozenset({12345, 12804138}): 48.996036529541016

If I provide the value 3245, I want to return

frozenset({12345, 3245}): 45.95948791503906, frozenset({3245, 9876}): 50.67853927612305

Basically, I want to iterate based one of the key from the multikey frozen set dictionary


Solution

  • You can try:

    my_dict = {frozenset({12345, 3245}): 45.95948791503906,
     frozenset({12345, 12804138}): 48.996036529541016,
     frozenset({3245, 9876}): 50.67853927612305}
    
    
    
    def retrieve_dict(number_I_want):
      final = []
      for key, value in my_dict.items():
        for i in key:
          if i == number_I_want:
            final.append(key)
      print(final)
    
    retrieve_dict(12345)