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
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.
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.
Here is an example with itemgetter:
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:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  Feed List items with Integer euras 9 594 May-19-2021, 07:45 PM
Last Post: snippsat
  Parse String between 2 Delimiters and add as single list items lastyle 5 620 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Sum of list items tristanfermat 2 530 Feb-06-2021, 12:04 PM
Last Post: tristanfermat
  Count number of occurrences of list items in list of tuples t4keheart 1 582 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  concatenating 2 items at a time in a python list K11 3 593 Oct-21-2020, 09:34 AM
Last Post: buran
  Select the other of 2 items in a list Clunk_Head 7 1,017 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Removing items from list if containing a substring pythonnewbie138 2 708 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Print the number of items in a list on ubuntu terminal buttercup 2 747 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  How to put the items of one list in new generated lists Bobbear 1 614 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 1,174 May-10-2020, 05:28 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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