-2

I have tried the below list_1 to arrange in a custom order ( based on the name of the keys):

example: each dict should be arranged in the same order of key names: please see the 'result'

list_1 = [{"three":3, "two":2, "four":4, "six":6, "five":5, "seven":7, "one":1}, {"six":6, "three":3, "seven":7, "one":1, "two":2, "four":4, "five":5}]
keys_list = ["one", "two", "three", "four", "five", "six", "seven"]

I am trying to get the above list of dict objects into the below one with custom order :

result_list =[{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7}, {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7}]

I could get the result with below code:

result_list=[]
result_dict={}
for i in list_1:

    for idx, j in enumerate(keys_list):  
        result_dict[keys_list[idx]] = i[j] 
    result_list.append(result_dict)
print(result_list) 

Can we improve by using lambda function?

mahen1812
  • 31
  • 4
  • 4
    The sort order you give is ordered by the value... not the key... – Jon Clements Apr 09 '22 at 13:57
  • 1
    Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers –  Apr 09 '22 at 15:29

4 Answers4

1

This is how you'd sort a single dictionary the way you want

dict(sorted(d.items(), key=lambda x: x[1]))

and here's how you'd sort all of them in a list

[dict(sorted(d.items(), key=lambda x: x[1])) for d in ls]
  • General style comment: `[x for x in ...]` is superfluous, unless you have an `if` clause in the comprehension: just use what's in `...` directly instead. Here, `sorted(d.items(), key=...)` does exactly the same thing. – joanis Apr 09 '22 at 16:46
  • And in the case where `...` is an iterable or an iterator you want to turn into a list, it's cleaner to use `list(...)` than `[x for x in ...]`. But I only do that when that iterable isn't going to work as is and a list is really needed, which is not often. – joanis Apr 09 '22 at 16:47
  • @joanis Yeah, that does make sense. I'll edit my answer. – Tecnical Compute Apr 09 '22 at 17:02
0

I think the easiest way would be by creating an array of keys first eg:

key_names = ["one","two","three"...]

Then you you create a new variable with the keys in each array using the .keys() function on list1 eg. new_var = list1[0].keys(). Next, you loop through each key name replacing with it's preferred value (), sort the new variable in ascending / descending since they are now numbers...

Caasi
  • 3
  • 4
  • I could get the solution using the below code: ```python result_list=[] result_dict={} for i in list_1: for idx, j in enumerate(keys_list): result_dict[keys_list[idx]] = i[j] result_list.append(result_dict) print(result_list) ``` It would be great if it can be done using the lambda function – mahen1812 Jun 04 '22 at 12:06
0

Python dictionnaries aren't meant to be ordered. To do so, you should use collections.OrderedDict. Full doc here : https://docs.python.org/3/library/collections.html#collections.OrderedDict

Then you could use a standard sorting algorithm to sort your dictonnary in any way you want.

Not than for python 3.6 and above, dictionnaries maintains insertion order by default, even though if the order is really important to you, OrderedDict are more suitable.

Black Raven
  • 134
  • 5
0
result_list=[]
for i in list_1:
    result_dict={}
    for idx, j in enumerate(keys_list):  
        result_dict[keys_list[idx]] = i[j] 
    result_list.append(result_dict)
print(result_list) 

I did some trial and error testing, and this solves my issue.

mahen1812
  • 31
  • 4