Use of the set and for function in the same structure

Asked

Viewed 61 times

0

I am studying data science through the Python language and came across the following code:

world_trends_set = set([trend['name'] for trend in world_trends[0]['trends']])
us_trends_set = set([trend['name'] for trend in us_trends[0]['trends']])

This example I took from the book "Mining-the-Social-Web-2nd-Edition". I can’t understand why the trend['name'] before the loop. I imagine the code is creating a set called name with the items in the first row of the column trends.

Can someone explain to me the advantage of using syntax this way and correct me if I’m wrong?

1 answer

2


Considering the line:

world_trends_set = set([trend['name'] for trend in world_trends[0]['trends']])

The equivalent code would be:

temp = []

for trend in world_trends[0]['trends']:
    temp.append(trend['name'])

world_trends_set = set(temp)

The result produced by both codes will be exactly the same, so yes, what the code is doing is creating a set from a list. This list, in turn, consists of the column name of value trend. That is to say, world_trends is a list where its position 0 is also a list of dictionaries that have the column name.

Whereas you are first creating a list, that is, storing all the values in memory, and then converting to a set, no performance gain or memory cost. The difference lies more in the writing of the code: the first form is smaller and even more readable to humans than the second. That is, the first way is easier to understand.

By the way, you can even remove the clasps around the expression for as follows:

world_trends_set = set(trend['name'] for trend in world_trends[0]['trends'])

Within the parentheses we would have a Generator Expression being converted to a set. I also would not present better performance, I believe, because convert the Generator for a set would also store all the elements in memory, but it would be one less operation for Python to process.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.