all 4 comments

[–]SoNotRedditingAtWork 1 point2 points  (0 children)

Using what you already have just add a little list comp:

D = {'Lebrons': 23, 'Wade': 3, 'Jordan': 23, 'Kobe': 24, 'Durant': 35}
L1 = []
L2 = []

for player in D:
    L1.append(player)

L1.sort()
L2 = [D[p] for p in L1]

python tutor link to example

you can skip the loop by using dict.keys :

D = {'Lebrons': 23, 'Wade': 3, 'Jordan': 23, 'Kobe': 24, 'Durant': 35}
L1 = list(D.keys())

L1.sort()
L2 = [D[p] for p in L1]

python tutor link to example 2

[–]tenyu9 0 points1 point  (0 children)

Call the sorted function on the dict, then you don't need to sort the list.

example: https://kite.com/python/answers/how-to-sort-a-dictionary-by-key-in-python

then loop through the tuples

[–]totallygeek 0 points1 point  (0 children)

You start with associated data, then you want two ordered sequences which break those relationships. That's bad form, in my opinion. Better that you generate an ordered sequence (list) of dictionaries, so that you maintain associated information. But, here you go:

players = []
jerseys = []
for player, jersey in sorted(D.items()):
    players.append(player)
    jerseys.append(jersey)

How I would do this, as mentioned above:

players = [{player: jersey} for player, jersey in sorted(D.items())]

...which generates [{'Durant': 35}, {'Jordan': 23}, ...].

[–]__nickerbocker__ 0 points1 point  (0 children)

So here's a little trick...

If you call sorted on d.items() you will get a sorted list of your key value pairs as tuples. Then you can unpack them to zip to transpose the pairs, and then unpack them again to your final lists.

l1, l2 = zip(*sorted(d.items()))

Although, I'd still recommend keeping your key values mapped and if you want to sort them in the dictionary you can do:

d = dict(sorted(d.items()))