Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists.sort() with cmp
#7
(Feb-06-2017, 06:41 PM)wavic Wrote: itemgetter() could be faster
No,you are guessing againĀ Wink
You can test,timeit work fine.
lambda is a lot faster.
# Python 3.6
import timeit

_lambda = '''\
lst = [('Tom', 'C', 10), ('Kent', 'A', 12), ('Jenny', 'B', 15)]
sorted(lst, key=lambda tup: tup[1])
'''
#--> 18.7 sec

_itemgetter = '''\
from operator import itemgetter
lst = [('Tom', 'C', 10), ('Kent', 'A', 12), ('Jenny', 'B', 15)]
sorted(lst, key=itemgetter(1))
'''
#--> 33.8 sec

print(timeit.Timer(stmt=_lambda).timeit(number=10000000))
Reply


Messages In This Thread
lists.sort() with cmp - by merlem - Feb-06-2017, 04:47 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 05:26 PM
RE: lists.sort() with cmp - by merlem - Feb-06-2017, 06:01 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 06:14 PM
RE: lists.sort() with cmp - by merlem - Feb-06-2017, 08:09 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-06-2017, 08:32 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-06-2017, 06:40 PM
RE: lists.sort() with cmp - by wavic - Feb-06-2017, 06:41 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 07:14 PM
RE: lists.sort() with cmp - by nilamo - Feb-08-2017, 07:29 AM
RE: lists.sort() with cmp - by merlem - Feb-09-2017, 02:05 PM
RE: lists.sort() with cmp - by wavic - Feb-09-2017, 02:16 PM
RE: lists.sort() with cmp - by merlem - Feb-09-2017, 03:29 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-09-2017, 09:12 PM
RE: lists.sort() with cmp - by snippsat - Feb-09-2017, 09:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,427 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 14,623 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,572 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,485 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Creating new list from 2 lists after a custom sort pythoneer 12 6,313 Jun-01-2018, 04:55 PM
Last Post: pythoneer

Forum Jump:

User Panel Messages

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