0

I am retyping the code examples in Joel Grus' "Data Science from Scratch: First Principles with Python." They are written in Python 2.7, but my installation is Python 3.6.0.

The following code block (transcribed from p. 5) generates a syntax error whenever I run it in Python 3.6:

# create a list (user_id, number_of_friends)
num_friends_by_id = [(user["id"], number_of_friends(user))
                 for user in users]

sorted(num_friends_by_id,                              # get it sorted
   key=lambda (user_id, num_friends): num_friends, # by num_friends
   reverse=True)                                   # largest to smallest

Specifically:

key=lambda (user_id, num_friends): num_friends, # by num_friends
           ^
SyntaxError: invalid syntax

From browsing StackOverflow and elsewhere, I'm guessing that the issue is the attempt to use (user_id, num_friends) with the lambda; all other examples I've seen seem to only provide a single parameter (if that's the right term), and it's not enclosed in parens. But I can't figure out (a) exactly what's wrong, and (b) how to tweak it so it works in 3.6.

I should note that I'm also confused by how Python is recognizing and doing anything with num_friends, given that said variable has never previously been defined (but number_of_friends has). However, since this is not listed in the book's errata, I am assuming there is a difference in the syntax between 2.7 and 3.6 that I'm not understanding.

TSSlade
  • 1
  • 1
  • You are using argument unpacking in a lambda, it is *that* which changed in Python 3. – Martijn Pieters Jun 27 '17 at 07:27
  • Thank you, @Martijn Pieters. So automatic tuple unpacking used to exist, but no longer does in 3.* Got it. Can you point me toward something that would help me understand how to obtain the desired outcome - sorting based on the num_friends value? I'm afraid I'm struggling to figure that out based on the responses to the question this was marked a duplicate of. – TSSlade Jun 27 '17 at 12:12
  • `lambda t: t[1]` should do just fine; you are passed a tuple of two elements and are sorting on the second element in that tuple. – Martijn Pieters Jun 27 '17 at 13:30
  • sorted(num_friends_by_id, key=lambda num_friends: num_friends, reverse=True) – H.Marroquin Jul 18 '20 at 02:17

0 Answers0