News Aggregator Tutorial Chapter 3 - Cat

Hello Anvil Forums!
I am a novice Python user and am working my way through the tutorials. I was hoping to get a few pointers on the code used in Chapter 3 of the News Aggregator tutorial at: https://anvil.works/learn/tutorials/database-backed-apps/chapter-3

I think my issue is there is some short forms or alias being used and I am just not familiar enough to catch it. The code is from this part:

categories = [
  (cat['name'], cat) for cat in app_tables.categories.search()
]

Later on it includes this bit of code as well:

self.category_box.items = categories

Working backwards from these two items I think I get the general idea. This bit of code

self.category_box.items = categories

is saying ā€œhey buds, you are going to change the contents of the category box you made earlier and fill it up with the stuff found in the categories variableā€.

For the next bit of code I get a bit of what it is saying but not sure how all of the areas were decided on. So for this bit

categories = [
(cat[ā€˜name’], cat) for cat in app_tables.categories.search()
]

it is saying ā€œhey buds, you are going to make a variable called categories and use stuff you find in the categories table. You are going to find a column called name in there and take stuff from thatā€. I guess I’m getting confused on the ā€œcat, cat, for catā€ part of this bit. When I type out the cat part it does give a quick access pop-up that says ā€œcat – ā€˜categories’ row, A row from the ā€˜categories’ table, View docsā€¦ā€ but I’m not sure how it went from a table name ā€œcategoriesā€ to having just ā€œcatā€. I don’t remember making an alias or shortcut for this. I tried changing the code to say ā€œcategoriesā€ and it works as well and made a bit more sense. How do I end up going from ā€œcategoriesā€ to just ā€œcatā€?

Welcome, @NescafeCoffee!

This kind of construct

 categories = [
    (cat['name'], cat)
    for cat in app_tables.categories.search()
]

is a Python comprehension. It’s a shortcut syntax for constructing a list. It could be written longhand like this:

categories = []
for cat in app_tables.categories.search():
    pair = (cat['name'], cat)
    categories.append(pair)

The for statement creates a variable cat, gives it the 1st value (database table row) from the search(), executes the body of the loop, and then moves on to the next result from search(). This continues until the series of results from search() is exhausted.

So it’s the for statement that actually creates variable cat. You can change its name to anything you like, as long as you change it consistently, and as long as you avoid names that are already in use at that time and place. For example:

categories = []
for category in app_tables.categories.search():
    pair = (category['name'], category)
    categories.append(pair)

Or, using the shorter (comprehension) syntax:

 categories = [
    (category['name'], cat)
    for category in app_tables.categories.search()
]
2 Likes

Thank you for the information. The long way of writing it ā€œclicksā€ a lot easier for me. Thanks for the help. Hope you have a good day!

1 Like

This was very helpful for me as well, thanks!

I agree with @NescafeCoffee that the longer way is easier to understand. Is there a reason to use comprehensions other than brevity?

1 Like