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.
source share