Python Forum
Alpha numeric element list search - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Alpha numeric element list search (/thread-25493.html)



Alpha numeric element list search - rhubarbpieguy - Apr-01-2020

I have a list question using the following script:

list1=['a','b','c','d','e','f']
list2=['1A','1B','1C','1a','1b','1c']
list3=[]
list4=[]

for i in list1:
for j in list2:
if i in j: list3.append(j)

for i in list1:
if i not in list3: list4.append(i)

List3 correctly contains:

1a
1b
1c

But list4 contains:

a
b
c
d
e
f

However, if I use ‘if i not in str(list3): list4.append(i)’ list4 correctly lists:

d
e
f

There’s apparently something basic in lists I don’t understand. The numeral 1 seems to cause the original code to fail. For if I delete the 1’s from list2, list4 displays correctly using the original code and not str(list3).

I understand 1 is an integer, but the elements in list2 are strings. ?


RE: Alpha numeric element list search - pyzyx3qwerty - Apr-01-2020

The elements that get displayed in list3 are '1a','1b','1c' not 'a','b','c'. Therefore, 'a','b','c','d','e','f' gets printed when you type if i not in list3. However, when you print if i not in str(list3), it looks for the elements inside the words and then prints the right answer.