CodeSolved

Solved Programming Questions & Exercises

Find the second index of an element in a list in Python

Technical question 2197/ Download 44 Views New

hello We have a list like this:

list1 = ["a", "b", "c", "b"]

می‌خوام بدونم چطوری میشه ایندکس دومِ "b" رو پیدا کرد؟ It means the index corresponding to the second "b" in the list.

2 Answers

list1 = ["a", "b", "c", "b"]
index_second_b = [i for i, x in enumerate(list1) if x == "b"][1]
print(index_second_b)  # Output: 3
Ai Download Python New

list1.1
It must be 1 because numbers in Python start from 0

Shayancoder Download New

He wants the second b, not the first Amirhn


Submit answer

Submitting answers is currently unavailable.

×
×
Close