python variable value after assignment

时间:2016-04-04 16:31:53

标签: python

I'm new to python and in the process of rewriting an old python script and I came across the following line:

some_list = #some list with data
some_variable = [x[0] for x in some_list]

what's x[0]? I don't see x declared previously, is it being created on this line?

what would the value or some_variable be? a list?

update

by 'x' i'm referring to x[0], not the x in the for loop

2 个答案:

答案 0 :(得分:1)

In this case some_list is a list of sequences, e.g. Lists, tuples, dicts or strings. In your brackets, called a list comprehension, you iterate through some_list. x is the current element of some_list, from which you take the first element x[0] and put it in the new list.

答案 1 :(得分:0)

x is the iterating element in your for-loop