0

I have the following dictionary:

{'key1': [value1], 'key2': [value2], 'key3': [value3], 'key4': [value4] }

what I want to do is to use the values to create a new dictionary. The value of the odd elements will be the new keys, and the value of the next element will be the new value of that key. I mean:

{'value1': [value2], 'value3': [value4]}

how can i do it? i would like to clarify that this is a small example, since dictionaries have hundreds of elements, therefore the solution should be scalable to dictionaries with any amount of elements.

ozo
  • 883
  • 1
  • 10
  • 18
  • How would we know that `value2` should be the value of the new key `value1`, etc.? (especially since dictionaries don't keep a certain order) – jcfollower May 18 '19 at 22:22
  • `dict` has no order in python. Therefore the notion of "next element" does not make sense. If by next element you actually mean "the next value, would this be a sorted array", then you might consider changing the way you store your data – qmeeus May 18 '19 at 22:22
  • @jcfollower then it's impossible? in the case of the dictionary I'm working with, whenever I print it on screen it keeps the same order. It is a dictionary generated by a request.POST – ozo May 18 '19 at 22:30
  • @qmeeus The dictionary I refer to is a request.POST. By default a POST request is given as a dictionary in django. – ozo May 18 '19 at 22:32
  • And convering to a sorted list is not an option? Something like that: `sorted(d.items(), key=lambda x: x[1])` – qmeeus May 18 '19 at 22:35
  • @qmeeus Let me test your code because I do not understand very well what it does, and I can answer :). now I have this doubt: if the dictionaries do not have order, why everytime I print different dictionaries generated by different request.POST, the dictionaries are printed with the same order? – ozo May 18 '19 at 22:52
  • The code I wrote is converting the dictionary into a list, sorted by the values in the dictionary. For the dictionary, it is actually a hashtable where the keys are stored as sets in python (which have no order). Look [here](https://stackoverflow.com/questions/526125/why-is-python-ordering-my-dictionary-like-so) for more information ;) – qmeeus May 18 '19 at 22:59

2 Answers2

1

Assuming, that 1) no KeyError will occur, i.e. there are no odd-indexed dictionary values with the same values, and 2) you have even number of entries in your dict, I would do something like this:

vals = my_dict.values()
new_dict = {}
for i in range(0, len(vals), 2):
    new_dict[vals[i]] = vals[i+1]

But, as others pointed out, dictionaries don't actually have order, so it's a "let's say" solution.

pmarcol
  • 453
  • 2
  • 9
-1

Steps to follow:

  1. Extract Keys Array from DictObj
  2. List them to category Even and Odd Indexed Array
  3. Create a dictionary out of these two array.

    dictObj = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4' };    
    print (dictObj.keys());
    evenArray =  dictObj.keys()[1::2];
    oddArray =  dictObj.keys()[::2];
    
    dictObjNew = dict(zip(evenArray, oddArray))
    print dictObjNew
    
rayryeng
  • 102,964
  • 22
  • 184
  • 193
shyamzzp
  • 115
  • 7
  • You gotta love python2 to stick to it at less than 1 year before the end of maintenance... https://pythonclock.org/ – qmeeus May 18 '19 at 23:02