-1
a=["four", "score", "and", "seven", "years"][[0,2,3][1]]

print(a)

output is : and

What is the main logic of this expression ?

Gingerbread
  • 53
  • 1
  • 4
  • 9

2 Answers2

3

You need to read such expressions from the inside out. Let's start with the original:

a = ["four", "score", "and", "seven", "years"][[0,2,3][1]]

[0,2,3] is a list, so you can access its elements with the [] operator. Since lists are zero-based, index 1 is the second item, which is 2:

a = ["four", "score", "and", "seven", "years"][2]

Here, again, we're accessing a list element by its index. 2 refers to the third element, which is "and".

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

[0,2,3][1] gives 2 which goes into ["four", "score", "and", "seven", "years"][2] which in turn returns and.

Nurjan
  • 5,889
  • 5
  • 34
  • 54