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.