0

Hi I am quite new to python and struggling with creating a for loop within a for loop. I want the bottom 3 lines to loop. Many thanks!

for restaurant in sorted_list:
    wks_res.append_row([
        restaurant["name"],
        restaurant["rating"],
        restaurant["user_ratings_total"],
        restaurant['reviews'][0]['text'],
        restaurant['reviews'][1]['text'],
        restaurant['reviews'][2]['text'],
])

The way I tried it is as follows:

for restaurant in sorted_list:
   wks_res.append_row([
       restaurant["name"],
       restaurant["rating"],
       restaurant["user_ratings_total"],
       for reviews in restaurant['reviews']:
          reviews['text'],

])
1
  • Does append_row specifically need a list, or can it be an arbitrary iterable? Commented Feb 24, 2019 at 16:17

3 Answers 3

3

This should work. Watch out for those brackets, they are rearranged on purpose. Check out list comprehension on Internet for more info about this.

for restaurant in sorted_list:
   wks_res.append_row([
       restaurant["name"],
       restaurant["rating"],
       restaurant["user_ratings_total"]] +
       [rev["text"] for rev in restaurant["reviews"]]

)
Sign up to request clarification or add additional context in comments.

Comments

1

you can't use a for loop inside an expression.

This should works :

for restaurant in sorted_list:
   # create the list to append
   row = [
       restaurant["name"],
       restaurant["rating"],
       restaurant["user_ratings_total"]
   ]
   # expend this list with 'reviews'
   for review in restaurant['reviews']:
          row.append(review['text'])
   # append the list to wks_res
   wks_res.append_row(row)

alternatively you can use list comprehension

row = [
   restaurant["name"],
   restaurant["rating"],
   restaurant["user_ratings_total"]
] + [review['text'] for review in restaurant['reviews']]

3 Comments

I think what you mean is that "you can't use a for loop inside an expression". Also, note that in Python 3, you can do this: row = [restaurant['name'], restaurant['rating'], restaurant['user_ratings_total'], *[review['text'] for review in restaurant['reviews']]. I'm not sure it's great from a readability point of view, but it's possible. :-)
I'd use operator.itemgetter here. Define get_stuff = itemgetter("name", "rating", "user_ratings_total" before the loop, then row = list(get_stuff(restaurant)) inside the loop. The nested loop can be replaced with row.extend(r['text'] for r in restaurant['reviews']).
If you really want to go crazy, row = list(chain(get_stuff(restaurant), (r['text'] for r in restaurant['reviews']))). (chain comes from the itertools module.)
0

Thanks! This indeed worked:

for restaurant in sorted_list:
   wks_res.append_row([
      restaurant["name"],
      restaurant["rating"],
      restaurant["user_ratings_total"]] +
      [rev["text"] for rev in restaurant["reviews"]]

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.