Python Forum
mydict.items() is not a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mydict.items() is not a list
#1
what i get from mydict.items() is not a list. i can't .sort() it. it have to make a list from it to be able to sort it in-place, defeating the performance advantage of sorting in place. what can be done to have the list of items in sorted order with that kind of performance?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
One can sort dictionaries with sorted(), not in-place though:

>>> d = {5: 'five', 3: 'three', 2: 'two'}
>>> sorted(d)
[2, 3, 5]
>>> sorted(d.items())
[(2, 'two'), (3, 'three'), (5, 'five')]
>>> dict(sorted(d.items()))
{2: 'two', 3: 'three', 5: 'five'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Here is an example with itemgetter: https://python-forum.io/Thread-Sort-last...#pid113226
This is useful, if you have a list with dicts and the dicts do have all the same keys.
Sorting them with the key function together with itemgetter is very handy.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count number of occurrences of list items in list of tuples t4keheart 1 108 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  concatenating 2 items at a time in a python list K11 3 161 Oct-21-2020, 09:34 AM
Last Post: buran
  Select the other of 2 items in a list Clunk_Head 7 448 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Removing items from list if containing a substring pythonnewbie138 2 353 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Print the number of items in a list on ubuntu terminal buttercup 2 420 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  How to put the items of one list in new generated lists Bobbear 1 315 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 568 May-10-2020, 05:28 AM
Last Post: deanhystad
  Add items from one list to list of lists PUP280 5 845 May-05-2020, 03:47 PM
Last Post: PUP280
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 688 Feb-21-2020, 02:40 PM
Last Post: Cosmosso
  counting items in a list of number combinations Dixon 2 458 Feb-19-2020, 07:06 PM
Last Post: Dixon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020