-1

I have a dictionary with key values, But I want to rearrange/change the type of the keys/values in the dict so as per my like. How can I do it?

for Example:

Dict = {'a':19,'b':30, 'c':"New", 'd': 6}

I want to make b as key at position 1 and/or I want to make a key as a list and so on. NOTE: I dont not want to sort the dict! But decide myself : Example:

If this key is present in dict then put this Key,value in Position 1

So my final result would be,

Dict = {'b':30, 'a':[19], 'd':6, 'c':"new"}
JRD
  • 849
  • 7
  • 14
  • Dictionaries (in Python 2.7) are *not* ordered, there is no concept of *position*, there is however `collections.OrderedDict` – Chris_Rands May 17 '18 at 08:05
  • So what would be the solution – JRD May 17 '18 at 08:06
  • Also why is `19` now in a list `[19]`? – Chris_Rands May 17 '18 at 08:06
  • I would like to make it as a list, its part of it – JRD May 17 '18 at 08:07
  • Why only `19` changes? How do you defined the new order? As I said you can use `OrderedDict` to preserve insertion order – Chris_Rands May 17 '18 at 08:08
  • I would like to mantain a certain order which has to be in a certain format to the convert in json. I get the key values from a dedicated output, and then based on the key values present , I want to order it especially depending on the keys i.e the type or order depends on the keys. 19 was just an example. it could be a dict or list of dict and so on – JRD May 17 '18 at 08:14
  • Since this question was closed, I can't suggest this as the official answer, but if you *need* your container to be a dictionary with arbitrary order, you "can" subclass Dict and then implement logic that would **dict**ate the algorithm by which you wish to represent the keys/values when interacting with the Dict. I'd probably just make a decorator and decorate functions that delegate to super(); unfortunately, the comment section is a little too small for a proper example. – Reid Ballard May 17 '18 at 17:57
  • @ReidBallard Thank you. Would look into it – JRD May 25 '18 at 10:40

2 Answers2

1

You can't put the key b in position 1, or any position. Dictionaries are unordered in principle. There is usually not much reason to care about the order.

But if you do care about the order then you have to maintain it yourself outside of the dict that holds your data. You can't insist on the dict maintaining the order itself, because dicts don't work that way. For example:

orderby =  ["b", "a", "d", "c"]
for key in orderby:
    do_something_with(key, Dict[key])

To change the order, for example to exchange the positions of "a" and "b":

orderby[0],orderby[1] = orderby[1],orderby[0]

There is also an OrderedDict in collections, but that is mainly to remember the order that things were added to it, which doesn't seem to be what you want.

To change the value associated with 'a' just assign it:

Dict['a'] = [19]
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • I would like to mantain a certain order . I get the key values from a dedicated output, and then based on the key values present , I want to order it especially depnding on the keys i.e the type or order depends on the key – JRD May 17 '18 at 08:11
  • You can get the keys out of the dictionary in an ordered sequence, you just can't insist on *storing* them that way. Do it like this: `for k in sorted(Dict.keys()): do_something_with(Dict[k])`. – BoarGules May 17 '18 at 08:13
  • If you don't want them in alphabetical order, but your own arbitrary order, then you need a second dictionary to store the order: `{0 :"b", 1: "a", 2:"d", 3:"c"}`, or a list `["b", "a", "d", "c"]`. – BoarGules May 17 '18 at 08:22
  • I quite didnt get you, But can you explian me in detail ? – JRD May 17 '18 at 09:08
  • @jaison I covered this in an edit to the question. If there is something there that you still don't understand then please say what it is. – BoarGules May 17 '18 at 11:44
  • I have a dict which which I have to alter the key and values with some other values/key and then I want to maintain a order as per my wish. – JRD May 17 '18 at 11:58
  • I have already explained that you can *not* make a `dict` store things in a particular order. They don't work that way. So if you want to keep track of the order you need a dict, or list, or *something*, that does that: in other words *you have to store the ordering information in a separate data structure of your own*. The edit to the answer gives a simple example of how to go about this. I understand that you want the `dict` to do this for you. I'm trying to get you to understand that it can't and won't, and so you need another approach. – BoarGules May 17 '18 at 12:03
0

The rearranging I think you cannot do as dictionaries are not ordered structures. The second one you can simply do by:

Dict['a'] = list(Dict['a'])
Ary
  • 424
  • 1
  • 4
  • 14