How to test if variable exists in nested list and append it to that list in python

danidee Source

Lets say for example i have a list called List1 which also contains two other lists (i.e nested list in python)

List1 = [['A'],['B']]

Then i have another list list called List2

List2 = ['A']

How do i iterate through the values of List1 and then check if List1 contains the value of List2 and append The value in List2 to index 0 in List1 which happens to be ['A']

This is what i've tried so far with enumerate but when i print List1 i keep on getting the same value [['A'],['B']]

List1  = [['A'],['B']]
List2 = ['A']

for idx, i in enumerate(List1):
    if List2 in i:
        List1.append(idx[i])

print(List1)

i want the result to be [['A','B'],['B']]

EDIT: i finally solved it and got the result i wanted

List1  = [['A'],['B']]
List2 = ['A']

for idx, i in enumerate(List1):
    if List2 in List1:
        List1[idx].append(List2[idx])

print(List1)
pythonlistpython-3.x

Answers

answered 3 years ago ericzundel #1

Rather than just write some code and tell you exactly how to solve this problem, I'll say that using the python debugger would be a big help to show you exactly what your code is doing.

First, you can manually set a breakpoint in your code by adding import pdb; pdb.set_trace() as follows:

List1  = [['A'],['B']]
List2 = ['A']

import pdb; pdb.set_trace()
for idx, i in enumerate(List1):
    if List2 in i:
        List1.append(idx[i])

print(List1)

Now, run your code:

$ python /tmp/t.py
> /tmp/t.py(5)<module>()
-> for idx, i in enumerate(List1):
(Pdb) 

Your code executed up to the breakpoint and stopped. Take a look around. the p or print command shows you the contents of variables:

(Pdb) p List1
[['A'], ['B']]
(Pdb) p List2
['A']
(Pdb) p List1[0]
['A']
(Pdb)

OK, everything looks good so far, now use the n or next command to step to the next line of code:

(Pdb) n
> /tmp/t.py(6)<module>()
-> if List2 in i:
(Pdb) p idx
0
(Pdb) p i
['A']

OK, this looks like what we expected. Sometimes I forget what the code around the current line looks like, that's when I use the l or list command:

(Pdb) list
  1     List1  = [['A'],['B']]
  2     List2 = ['A']
  3     
  4     import pdb; pdb.set_trace()
  5     for idx, i in enumerate(List1):
  6  ->     if List2 in i:
  7             List1.append(idx[i])
  8     
  9     print(List1)
[EOF]

Use n again to go to the next line of code:

> /tmp/t.py(5)<module>()
-> for idx, i in enumerate(List1):
(Pdb)

It started at the next iteration of the for loop! Were you expecting this to execute line 7 next? Why didn't it?

Use q or quit to exit the debugger, then let's start over and go back to line 6 and then evaluate the expression in the debugger:

(Pdb) q
... 
bdb.BdbQuit
$ python /tmp/t.py
> /tmp/t.py(5)<module>()
-> for idx, i in enumerate(List1):
(Pdb) n
> /tmp/t.py(6)<module>()
-> if List2 in i:
(Pdb) p List2
['A']
(Pdb) p i
['A']
(Pdb) p List2 in i
False

Does that surprise you? The issue you're having is that it is looking for an array containing 'A' in the list.

(Pdb) p ['A'] in i
False

For that to be true i would have to look like this:

(Pdb) p ['A'] in [['A']]
True

What you really wanted to ask was:

(Pdb) p 'A' in i
True

I hope that helps you debug your code!

  • Note that I had to fix several typos to run your example. Check your code to make sure it runs before posting your question next time.

comments powered by Disqus