-4
 example: newlist = ["a,b,c","d","e","f","g"]

What is the position of b ? because normally we just input newlist[0][1] to find the elements of the list

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Alexx
  • 1
  • 1

2 Answers2

0

You can try something like this:

search = 'b'
for i in newlist:
    for j in i:
        if j == search:
            print(str(i.index(search)) + str(newlist.index(i)))

That should get you: 0 1. But this will only work if you have a 2d list.

WholesomeGhost
  • 1,101
  • 2
  • 17
  • 31
0

We can use enumerate for this:

new_list = ["a,b,c","d","e","f","g"]

for index, item in enumerate(new_list):
     print(index, item)
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 pos.py 
0 a,b,c
1 d
2 e
3 f
4 g

Then we can use an if statement to retrieve the index:

for index, item in enumerate(new_list):
    if 'b' in item:
        print(index)
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 pos.py 
0
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20