0

I saw this sample code in the book "Data Science from Scratch" by Joel Grus.

def friends_of_friend_ids_bad(user):
  # "foaf" is short for "friend of a friend"
  return [foaf["id"]
          for friend in user["friends"] # for each of user's friends
          for foaf in friend["friends"]] # get each of _their_ friends

However I can not find this specific language construct or syntax of for loop in Python guides I have seen. The for loop is without statements and it looks like the function above is trying to return an array of ids of friend of friends by iterating over the individual arrays. I would still like to know the formal language construct that allows this kind of short hand to iterate over array and extract elements.

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131

0 Answers0