Python – sorted expected 1 argument, got 3 – Trying to understand named arguments
Question:
I was looking into the sorted()
function in Python 3.9 with the following tiny example
circus = [('a', 'b', 'c', 'd', 'e'),('w', 'x', 'y', 'z', 'a'),('k', 'l', 'm', 'n', 'o'),('u', 'v', 'w', 'x', 'y'),('q', 'r', 's', 't', 'u'),('e', 'f', 'g', 'h', 'i')]
It’s just a list of quintuples that I want to sort by the fifth elements (i.e. the e, a, o, y, u, i)
I know the correct way to do this is
sorted(circus, key = lambda d: d[4], reverse = True)
But I was trying this
sorted(circus, lambda z: z[4], True)
and got the error
TypeError: sorted expected 1 argument, got 3
I’m trying to understand why it’s expecting 1 argument. According to the documentation (https://www.w3schools.com/python/ref_func_sorted.asp), the other two are optional parameters, but they should still be expected, right?
Answers:
If you look at the signature for sorted
(help(sorted)
)
sorted(iterable, /, *, key=None, reverse=False)
you’ll see the iterable parameter is positional only, while the other two are keyword-only. You can’t use a keyword argument like iterable=[1,2,3]
to provide the iterable to sort, and as you’ve seen, you can’t specify the key function or the reverse flag using positional arguments.
The syntax for parameter definitions can be found in the documentation for the def
statement:
Parameters after “*
” or “*identifier
” are keyword-only parameters and may only be passed by keyword arguments. Parameters before “/
” are positional-only parameters and may only be passed by positional arguments.
I was looking into the sorted()
function in Python 3.9 with the following tiny example
circus = [('a', 'b', 'c', 'd', 'e'),('w', 'x', 'y', 'z', 'a'),('k', 'l', 'm', 'n', 'o'),('u', 'v', 'w', 'x', 'y'),('q', 'r', 's', 't', 'u'),('e', 'f', 'g', 'h', 'i')]
It’s just a list of quintuples that I want to sort by the fifth elements (i.e. the e, a, o, y, u, i)
I know the correct way to do this is
sorted(circus, key = lambda d: d[4], reverse = True)
But I was trying this
sorted(circus, lambda z: z[4], True)
and got the error
TypeError: sorted expected 1 argument, got 3
I’m trying to understand why it’s expecting 1 argument. According to the documentation (https://www.w3schools.com/python/ref_func_sorted.asp), the other two are optional parameters, but they should still be expected, right?
If you look at the signature for sorted
(help(sorted)
)
sorted(iterable, /, *, key=None, reverse=False)
you’ll see the iterable parameter is positional only, while the other two are keyword-only. You can’t use a keyword argument like iterable=[1,2,3]
to provide the iterable to sort, and as you’ve seen, you can’t specify the key function or the reverse flag using positional arguments.
The syntax for parameter definitions can be found in the documentation for the def
statement:
Parameters after “
*
” or “*identifier
” are keyword-only parameters and may only be passed by keyword arguments. Parameters before “/
” are positional-only parameters and may only be passed by positional arguments.