0

Say I want the index of a list, with sublists, of the sublist with the first item as 3: therefore I want index of 2. Ideally I could use index:

mylist = [[1,3],[2,2],[3,7]]
# Sadly this is not possible: 
i = mylist.index(3, key = lambda x: x[0])
>>> 2

But sadly index method does not support a key parameter. Are there any pythonic functions which can do the same job?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Zak Stucke
  • 432
  • 6
  • 18
  • 2
    `[x[0] for x in mylist].index(3)`? Or if you are worried about space efficiency, use a for-loop... – juanpa.arrivillaga May 04 '18 at 18:00
  • juanpa.arrivillaga Yep this would work but was hoping there was some generic function I haven't been able to find that allows some sort of key parameter that I've shown above. – Zak Stucke May 04 '18 at 18:07
  • 1
    There is no built-in list method that does this, if that is what you are asking. See the linked duplicate for various approaches that work generically for any iterable, if that is what you mean. Again, I would probably just use a for-loop. – juanpa.arrivillaga May 04 '18 at 18:09
  • That's a shame but thankyou I will just use the for loop! – Zak Stucke May 04 '18 at 18:12

0 Answers0