0

I have a dictionary in the following format: Key (string) : Value (list[string])

my_dict = {'Foo': ['Lorem', 'Ipsum', 'Dolor', 'Baz'], 'Bar': ['Amet', 'Consectetur'], 'Baz': ['...'], 'Lorem': ['...'], & so on...}

I want to access this dictionary by indexing each key, such that Foo = 1, Bar = 2, Baz = 3, Lorem = 4, Ipsum = 5.. and so on

I want to choose a key by index, then pick a value, and go to that index, and so on.

For example: If I pick 1, I will go to Foo. Then from Foo, I will go to either Lorem, Ipsum, Dolor, or Baz.

I am essentially creating another dictionary dict2, which will hold a number of integers as the new key, and the key from dict1 as its value.

First I tried to call dict2.update({int_list:dict1.keys()) but this resulted in an unhashable type error. Ok, so I converted the list into a tuple and it updated but did not result in the dictionary I wanted.

What is an appropriate way to go about doing this?

Peter Parker
  • 43
  • 1
  • 6
  • 1
    This reads like you want a graph. – warped Mar 31 '22 at 18:06
  • Does the "dict indexing" code need to keep up to date with changes to underlying `my_dict`, or is `my_dict` unchanging? – wim Mar 31 '22 at 18:06
  • Read through: https://stackoverflow.com/questions/36244380/enumerate-for-dictionary-in-python – PM 77-1 Mar 31 '22 at 18:08
  • Related: [Accessing dictionary items by position in Python 3.6+ efficiently](https://stackoverflow.com/q/52507860/674039) – wim Mar 31 '22 at 18:09
  • @wim Either, I think. I will have (n) random look up functions called on the dict. If two 'lookups' are ever at the same key, then this key is eliminated as a possible route from the dict and lookup is abandoned. I hope this makes sense! – Peter Parker Mar 31 '22 at 18:13
  • @PeterParker What I'm asking is whether `my_dict` will have items added or removed (in which case the lookup needs to keep in sync), or it is unmodified (in which case the lookup can be created once) – wim Mar 31 '22 at 18:22

3 Answers3

0

Use enumerate() to pair each index with a key, then convert that to a dictionary:

dict2 = dict(enumerate(my_dict, 1))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This will be very inefficient if dict2 needs to be kept in sync with my_dict, though. – wim Mar 31 '22 at 18:07
  • 1
    That's a fault of his general idea of having the second index. I'm not sure why he needs that. – Barmar Mar 31 '22 at 18:08
0

First, you might need to get the dic.items() and loop through, you will need list to append the values of the other list, this is the code, below:

my_dict = {'Foo': ['Lorem', 'Ipsum', 'Dolor', 'Baz'], 'Bar': ['Amet', 'Consectetur']}

  key_list = list()
  dic = dict()
  total = list()
  for key, groups in my_dict.items():
    joined_list = my_dict['Foo'] + my_dict['Bar']
    print(f'{key}: {groups}')
    key_list.append(key)
    dic[key] = key.count(key)
    s = key_list.index(key) + 1
    dic[key] = s
    for index, value in enumerate(joined_list):
        # print(index, value)
        total.append(index)
        dic[value] = index + 1 + s
  print(dic)
derek
  • 21
  • 7
  • please @PeterParker note that for the keys which was represented by the actual key(my_dict['Foo'] & my_dict['Bar']); you can use dict.keys() to get each key in the loop. – derek Mar 31 '22 at 21:13
0

Define a global index variable and use the the walrus operator, doc, to increment it in a dictionary comprehension. If the keys should start from 1 set the global index to 0.

my_dict = {'Foo': ['Lorem', 'Ipsum', 'Dolor', 'Baz'], 'Bar': ['Amet', 'Consectetur'], 'Baz': ['...'], 'Lorem': ['...']}

i = 0   # global index
d = {(i:=i+1): v for v in my_dict.values()}
cards
  • 3,936
  • 1
  • 7
  • 25