Python Forum
Alpha numeric element list search
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alpha numeric element list search
#1
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. ?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sume Every 10 element in the list and Divided of Sum quest_ 7 135 Yesterday, 10:58 AM
Last Post: perfringo
  Counting Element in Multidimensional List quest_ 1 91 Nov-25-2020, 10:00 PM
Last Post: quest_
  Adding List Element if Second part of the List Elements are the Same quest_ 3 121 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Removing element from list squall 6 171 Nov-22-2020, 09:34 PM
Last Post: jefsummers
  2d Array adds last element to entire list waiteup 2 96 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  How to get index of minimum element between 3 & 8 in list Mekala 2 162 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Problem printing last element from a list tester_V 3 196 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Finding an element in a 1d list in a 2d array lionrocker221 0 372 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  removing dictionary element in list using (key, value) MelonMusk 3 436 Jun-13-2020, 02:37 PM
Last Post: buran
  Search in an unsorted list amir_0402 2 440 Jun-04-2020, 10:25 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020