You're right .;) They are difficult to read, which can also make them difficult to collect and debug. Let's look at the following example:
current_team = dict((k,v) for k,v in list(team.items()) for player in v['player'] if player['year'] == 2013)
Too many years of programming in C and Java made it difficult for me to read. Understanding is logically divided into different parts, but I still need to really look at it in order to decompose it.
The main thing to remember is that understanding is an expression, not a statement. That way, you can surround the expression with parsers, and then use implicit line concatenation to add line breaks that organize the expression based on its nesting levels:
current_players = (dict((k,v) for k,v in list(team.items()) for player in v['player'] if player['year'] == 2013))
It becomes clearer here that "the last index is the fastest, just like nested for loops. "
You can even add blank lines and comments:
current_players = (dict((k,v)
One note: the Python manual says that "indenting continuation lines doesn't matter." Thus, you can use any form of indentation to improve readability, but the interpreter will not perform any additional checks.
source share