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
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
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.
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