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?