Home python for loop with if statement not returning items
Reply: 3

python for loop with if statement not returning items

cfinspan Published in 2018-02-13 21:02:38Z

I am trying to learn python through some basic exercises with my own online store. I have a list of parts that are in-transit to us that we have already ordered, and I have a list of parts that we are currently out of stock of. I want to be able to send a list to the supplier of what we need - but I do not want to create duplicate orders as a result of the fact that the parts on order, are listed as out of stock.

I put together this basic program that looks through the list of items that are out of stock and only prints the item if it is present in the outofstock list but not present in the onorder list, so that if it is on order we do not order it again. However, it outputs nothing.

onorder = ["A1417", "A1322", "ISL6259", "LP8545B1SQ", "PM6640", "SLG3NB148V", "PD4HDMIREG", "338S1201", "SN2400B0", "AD7149", "J3801", "J4502", "IPRO97B"]
outofstock = ["ISL6259", "LY-UVH900", "triwing", "banana-to-alligator", "LP8548B1SQ", "EDP-J9000-30-PIN-IPEX", "J3801", "LT3470", "PM6640", "SN2400B0", "IPRO97B", "SLG3NB148V", "SN2400AB0", "usbammeter", "821-00814-A", "J5713", "343S0645", "PMCM4401VPE", "J4502", "PMD9645", "J9600", "J2401", "AD7149", "593-1604", "821-1722", "LM3534TMX", "U4001"]

for part in onorder:
    if (part in onorder) == False and (part in outofstock) == True:
    print (part)

It doesn't print anything, even though there are entries in outofstock that are not in onorder.

If I try this outside of a loop, it works and prints every part in the onorder list.

for part in onorder:
    print (part)

If I try this outside a loop, it also works and prints triwing, since it is true that triwing is in the outofstock list.

if ('triwing' in outofstock) == True:
    print ("triwing")

However, the program in the for loop returns nothing. What am I missing?

Monkey Supersonic
Monkey Supersonic Reply to 2018-02-13 21:12:12Z
for part in onorder:
    if (part in onorder) == False ...

This does not make sense. Since you are iterating over exactly every element of onorder, you will never get a part not in onorder. Therefore, it is not a miracle that the print statement is not being executed.

Barmar Reply to 2018-02-13 21:26:36Z

You're looping over the wrong list. To find items in outofstock but not in onorder, loop over outofstock:

for part in outofstock:
    if part not in onorder:

Simpler would be to convert both lists to sets, and compute the difference:

print(set(outofstock) - set(onorder))
cfinspan Reply to 2018-02-13 21:28:30Z

Doh! Appropriate code was

for part in outofstock:
if (part not in onorder):
    print (part)

This way it prints my out of stock items which I need to order, unless they were already on order.

I can't believe I overly complicated this for no good reason. Thank you so much for pointing out where I had gone wrong. This was such a dumb question in hindsight.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.365535 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO