0

How can I print indexes number of a given number in the list?

L=[1,3,8,3,12,3] X=3 output:1 3 5

4th Jun 2020, 6:42 PM
Almasa Sense
Almasa Sense - avatar
13 Respostas
+ 5
for i,j in enumerate(L): if j==X: print(i)
4th Jun 2020, 6:49 PM
Abhay
Abhay - avatar
+ 5
If you like to have the indexes found stored in a list, you can do like this: L=[1,3,8,3,12,3] res = [ind for ind,num in enumerate(L) if num == 3] print(res) #output: [1, 3, 5]
4th Jun 2020, 8:27 PM
Lothar
Lothar - avatar
+ 2
L.index[3] or x=len(L) for I in range(x): if L[I] ==3: print(I)
4th Jun 2020, 6:46 PM
Olivia
Olivia - avatar
0
and use this to print all in the same line print(i ,end="") Out put: 1 3 8 3 12 3
4th Jun 2020, 6:46 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
0
You can make a loop to iterate from 0 to list length. In each iteration you check if the wanted item is located at the index, which is the iteration number. If it is, print the iteration number.
4th Jun 2020, 6:48 PM
Seb TheS
Seb TheS - avatar
0
I do that but it he print 1 11 -->the first index
4th Jun 2020, 6:49 PM
Almasa Sense
Almasa Sense - avatar
0
I want the indexes 3 in list
4th Jun 2020, 6:52 PM
Almasa Sense
Almasa Sense - avatar
0
L=[1,3,8,3,12,3] X=int(input()) for i in L: if x==i: Print(L. index(I))
4th Jun 2020, 6:59 PM
Almasa Sense
Almasa Sense - avatar
0
It print 1 1 1
4th Jun 2020, 7:00 PM
Almasa Sense
Almasa Sense - avatar
0
Thanks olivia it's work 😘
4th Jun 2020, 7:07 PM
Almasa Sense
Almasa Sense - avatar
0
Your welcome 😀
4th Jun 2020, 7:25 PM
Olivia
Olivia - avatar
0
for i in range(5): if L[i]==value: print(i)
6th Jun 2020, 5:44 PM
Iqra Rehman
- 1
L=[1,3,8,3,12,3] for i in L: print(i) out put: 1 3 8 3 12 3
4th Jun 2020, 6:44 PM
Muhammad Galhoum
Muhammad Galhoum - avatar