Why a for loop?

It’s a bit confusing when written like so because there is an implicit tuple there. Python treats the expression as [i for (i, x) in enumerate(my_list) if my_list[0] == x].

So its NOT a list with these 2 members:

  1. i for i
  2. x in enumerate(my_list) if my_list[0] == x

Instead, it is equivalent to:

result = []
for iteration in enumerate(my_list):
    (i, x) = iteration
    if x == my_list[0]:
        result.append(i)
     
1 Like