How to sort a dictionary in python by value when the value is a list, and I want to sort it by the first index of this list

it is possible to sort the python dictionary by value if that value is a list, and I want it to be sorted by the first value of this list. For instance:

data = {
"Joe" : [1, "Joe", "password", "Joe@Email.com"], 
"Toby" : [2, "Toby", "password", "Toby@Email.com"], 
"John" : [4, "John", "password", "John@Email.com"], 
"Julie" : [3, "Julie", "password", "Julie@Email.com"]
}

I would like it to be like this: where "i" is the key

for i in range(len(data)):
    print("UserID: ", str(data[i][0]), ". Username: ", data[i][1])

>> UserID: 1. Username: Joe
>> UserID: 2. Username: Toby
>> UserID: 3. Username: Julie
>> UserID: 4. Username: John

Many thanks.

+4
source share
2 answers

You cannot sort the dict in place because Python dicts is unordered. You have at least 2 options:

Create a sorted list of tuples

You can use sortedwith an argument key=. In this case, this will be the first element of the dict value:

sorted(data.items(), key= lambda x: x[1][0])
# [('Joe', [1, 'Joe', 'password', 'Joe@Email.com']), ('Toby', [2, 'Toby', 'password', 'Toby@Email.com']), ('Julie', [3, 'Julie', 'password', 'Julie@Email.com']), ('John', [4, 'John', 'password', 'John@Email.com'])]

, :

data = {
    "Joe": [1, "Joe", "password", "Joe@Email.com"],
    "Toby": [2, "Toby", "password", "Toby@Email.com"],
    "John": [4, "John", "password", "John@Email.com"],
    "Julie": [3, "Julie", "password", "Julie@Email.com"]
}

for name, lst in sorted(data.items(), key=lambda x: x[1][0]):
    print("UserID : %d. Username : %s" % (lst[0], name))

# UserID : 1. Username : Joe
# UserID : 2. Username : Toby
# UserID : 3. Username : Julie
# UserID : 4. Username : John

data dict, OrderedDict:

from collections import OrderedDict

data = {
    "Joe": [1, "Joe", "password", "Joe@Email.com"],
    "Toby": [2, "Toby", "password", "Toby@Email.com"],
    "John": [4, "John", "password", "John@Email.com"],
    "Julie": [3, "Julie", "password", "Julie@Email.com"]
}

data = OrderedDict(sorted(data.items(), key=lambda x: x[1][0]))
# OrderedDict([('Joe', [1, 'Joe', 'password', 'Joe@Email.com']), ('Toby', [2, 'Toby', 'password', 'Toby@Email.com']), ('Julie', [3, 'Julie', 'password', 'Julie@Email.com']), ('John', [4, 'John', 'password', 'John@Email.com'])])

. key=lambda x: x[1].

+6

itemgetter, ( ). , , .

import operator

data = {
"Joe" : [1, "Joe", "password", "Joe@Email.com"], 
"Toby" : [2, "Toby", "password", "Toby@Email.com"], 
"John" : [4, "John", "password", "John@Email.com"], 
"Julie" : [3, "Julie", "password", "Julie@Email.com"]
}


sorted_data = sorted(data.items(), key=operator.itemgetter(1))

for entry in (sorted_data):
    print("UserID: " + str(entry[1][0]) + ". Username: " + entry[0])

:

UserID: 1. Username: Joe
UserID: 2. Username: Toby
UserID: 3. Username: Julie
UserID: 4. Username: John

. sorted_data - , , python . . : python ?

+3

Source: https://habr.com/ru/post/1672173/


All Articles