user1749431
user1749431

Reputation: 569

Most efficient way to get indexposition of a sublist in a nested list

I want to get the indexposition of a sublist in a nested list, e.g 0 for [1,2] in nested_ls = [[1,2],[3,4]]. What's the most elegant way of getting the index of the sublist, is there something similar to index() ?

Upvotes: 0

Views: 226

Answers (1)

user2357112
user2357112

Reputation: 280963

Yes. It's called index.

>>> [[1, 2], [3, 4]].index([1, 2])
0
>>> [[1, 2], [3, 4]].index([3, 4])
1
>>> [[1, 2], [3, 4]].index([1, 5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: [1, 5] is not in list

Upvotes: 5

Related Questions