PEP 8 and list comprehension

I seem to be facing a PEP 8 problem.

I use a list that is longer than 79 characters. So my text editor is screaming to me to do something, and it is an eye to watch when coding.

enter image description here

return [(i['user_id'], i['id']) for i in j['collection'] if i and i['user_id']] 

So, I'm trying to break the line, but now she complains that my line break is for visual purposes.

enter image description here

 return [(i['user_id'], i['id']) for i in j['collection'] if i and i['user_id']] 

What man should do in such a pickle?

Edit: Based on the answers, I chose readability over the list in this case, and now it makes sense to the reader:

 tracks = set() for track in json['collection']: if track and track['user_id']: tracks.add((track['user_id'], track['id'])) 
+5
source share
4 answers

Using common sense pep8. If you cannot put a line of logic within 80 characters, it is almost always a sign that this line needs to be rewritten. And this is true in this case, because I can’t even start trying to figure out what this line should do.

But if you move it to a much more readable format, then not only your line counter will grow, but your readability will also leave you plenty of room for the right variable names, such as user instead of i . This will make your own service in the future much easier, not to mention if anyone else ever looks at it.

So, to bring this example into real code, here is what I would do with it (with a good porting of functions to make fetching a lot easier to read!):

 j = {'collection': [{'id': 1, 'user_id': 1}]} def get_qualified_users(users): qualified_users = [] for user in users: if user and user['user_id']: qualified_users.append((user['user_id'], user['id'])) return qualified_users print(get_qualified_users(j['collection'])) 

You can easily copy / paste it into your interpreter to make sure that it works. And what's more, it is very easy to maintain and follow, with an explicit api enclosed in a hermetic function.

+6
source

if you want to stick strictly to pep8, then hanging padding is your friend.

 return [ (i['user_id'], i['id']) for i in j['collection'] if i and i['user_id']] 
+4
source

While I am a fan of the strict restriction of the 80s char and (as mentioned in the commentary), so Raymond Hettinger in this convincing conversation from PyCon 2015, in this particular case, the purest thing is to simply split it into two lines:

 valid_things = (i for i in j['collection'] if i and i['user_id']) return [(i['user_id'], i['id']) for i in valid_things] 

Thus, the logic of "filtering" and "choice" is clearly separated and seems to be in a more readable way than their merging.

Another option for functionally inclined:

 return map(itemgetter('user_id', 'id'), valid_things) 
+1
source

You just need to correctly defer the second line:

 return [(i['user_id'], i['id']) for i in j['collection'] if i and i['user_id']] 

Confirmed using PEP8 online, but let me know if this will work for you.

Personally, I don't like the expression and the source together, but the condition is shared. I would rather clearly see the expression, not the condition. Highlight what you get. Therefore, I would do one of the following:

 return [(i['user_id'], i['id']) for i in j['collection'] if i and i['user_id']] return [(i['user_id'], i['id']) for i in j['collection'] if i and i['user_id']] 

Clarification: this is how I stepped back / broke. I did not consider variable names, because I just wanted to give a direct answer and explain what the PEP8 error is and how to fix it, because no one else had it.

0
source

All Articles