'How to reorder the keys of a dictionary?
I have multiple dictionaries inside the list. I want to sort the dictionary with the custom key. In my case, I want to sort it using Date key. By that, I mean to move the Date key to the first position. What is the efficient way to sort the dictionary using Date key?
PS: I don't want to sort by the value of the Date.
[
{
"AmazonS3":6.54,
"AmazonEC2":27.55,
"AmazonCloudWatch":0.51,
"Date":"2020-07-01"
},
{
"AmazonEC2":27.8,
"Date":"2020-07-02"
},
{
"AmazonElastiCache":0.01,
"AmazonEC2":35.34,
"Date":"2020-07-03"
}
]
Expected output:
...
{
"Date":"2020-07-03",
"AmazonElastiCache":0.01,
"AmazonEC2":35.34
}
...
Solution 1:[1]
I believe you can use OrderedDict's move_to_end to do this.
dict = OrderedDict.fromkeys("qwerty")
dict.move_to_end("t", last=False)
"".join(dict.keys())
"tqwery"
Solution 2:[2]
Add the following style to css file.
#primary section.entry-content{ max-width: 100%; }
it will solve the issue
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Spaceglider |
| Solution 2 | Sunil |
