-1

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

duxfox--
  • 9,097
  • 15
  • 55
  • 116

2 Answers2

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.

Jesse Bakker
  • 2,143
  • 11
  • 23
0

x is the iterating element in your for-loop

Alessandro
  • 735
  • 9
  • 21