Precautions when passing def to sorted and groupby functions in Python? ??

I'm studying Python, but I got hooked and got stuck for about an hour. I solved it for the time being, but I can't help but if anyone understands this mechanism, I would be grateful if you could teach me, so I will write it.

The following is a program that mixes Los Angeles and New York players with a combination of player names and blocks and divides them into A block and B block respectively.

groupLeague.py


import itertools
LA = [('Jake', 'B'), ('Elwood', 'A')]
NY = [('James', 'A'), ('Carry', 'B'), ('Steven', 'B')]
data = itertools.chain(LA, NY)

def getSortKey(item):
    return item[1]

grp = itertools.groupby(sorted(data, key=getSortKey(data), getSortKey(data))

But when I do this, TypeError: 'itertools.chain' object is not subscriptable Will be played with an error.

I didn't understand it at all, and I was really into it, but when I looked at another sample program and revisited it, it worked. In the last line,

grp = itertools.groupby(sorted(data, key=getSortKey(data)), getSortKey(data))

,

grp = itertools.groupby(sorted(data, key=getSortKey), getSortKey)

And, except for the argument of getSortKey, it worked fine.

The program worked for the time being, but I have no idea about this mechanism. I understand that you can specify a "function that can get a key" in addition to the key value as the second argument "key part" of groupby, but when you specify a function, why do you not have to write an argument? Or is it an error if I write an argument? ?? As far as I can confirm the operation, I think that the first argument "iterable part" (sorted ~) is automatically treated as an argument, but there was no such description in the document.

【reference】 Python documentation itertools --- Iterator generation function for efficient loop execution

The same is true for the sorted function. In the part of key = getSortkey, the argument seems to automatically take the data of the first argument, but I do not understand the mechanism (I did not mention it in the document after all).

【reference】 Python documentation built-in functions sorted

What kind of mechanism is this without taking arguments ...? ?? I looked it up, but I just couldn't figure it out. I'm very sorry that this is a rudimentary question, but if you know it, it would be greatly appreciated if you could teach me.

How can we deal with this kind of tricky specification?

Recommended Posts

Precautions when passing def to sorted and groupby functions in Python? ??
Python 3 sorted and comparison functions
Comparison of how to use higher-order functions in Python 2 and 3
Precautions when using pit in Python
About python dict and sorted functions
How to use functions in separate files Perl and Python versions
Precautions when giving default values to arguments in Python function definitions
Precautions when pickling a function in python
How to use is and == in Python
[Tips] Easy-to-read writing when connecting functions in Python
How to generate permutations in Python and C ++
Precautions when dealing with control structures in Python 2.6
[Python] Precautions when assigning values to multidimensional arrays
Send messages to Skype and Chatwork in Python
Practice applying functions and global variables in Python
Error when trying to install psycopg2 in Python
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
[Python] Precautions when retrieving data by scraping and putting it in the list
Sorted list in Python
Convert timezoned date and time to Unixtime in Python2.7
Things to keep in mind when developing crawlers in Python
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
Precautions when dealing with ROS MultiArray types in Python
Things to keep in mind when copying Python lists
Things to note when initializing a list in Python
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Write tests in Python to profile and check coverage
[Python] How to sort dict in list and instance in list
Precautions when adding setter and deleter in derived class to getter property of base class
How to exit when using Python in Terminal (Mac)
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
I want to do something in Python when I finish
[Python] When you want to import and use your own package in the upper directory
Precautions when inputting from CSV with Python and outputting to json to make it an exe
Script to count and stop up to 5 seconds in Python in Blender
Try to make it using GUI and PyQt in Python
Application to display and search local memos (diary) in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Display numbers and letters assigned to variables in python print
Tips for coding short and easy to read in Python
[Python] Summary of how to use split and join functions
A standard way to develop and distribute packages in Python
Useful tricks related to list and for statements in Python
Convenient writing method when appending to list continuously in Python
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Problems and solutions when asked for MySQL db in Python 3
Things to keep in mind when using Python with AtCoder
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Precautions when changing unix time to datetime type in pandas
Things to keep in mind when using cgi with python.
How to execute external shell scripts and commands in python
Just try to receive a webhook in ngrok and python
How to log in to AtCoder with Python and submit automatically
Let's use def in python
Precautions when upgrading TensorFlow (to 1.3)
To flush stdout in Python
Overriding library functions in Python