1

I have a list and I am comparing to check if certain set of values exist in that list and return back true or false.

Given below is what I have tried:

l1 = ['apples,oranges,bananas,pears']   <<- list 1
l2 = ['apples,'tomatoes']               <<- list 2
b2 = set(l1).intersection(l2)       <<- comparing the 2 lists

On performing the above I get an error:

TypeError: 'NoneType' object is not iterable

I believe the above error means there is no data but I know the second list (l2) does have a value. Even if one value in the list matches I would like to get a True flag. Could anyone assist. Thanks

4
  • 2
    Cannot reproduce. Please paste the actual code you're running. Commented Nov 25, 2018 at 19:10
  • 2
    As you have typed it, l2 is missing a single quote after the word apples. Not sure if that was just a typo here or actually in your code... Commented Nov 25, 2018 at 19:10
  • 1
    You have mistake in your list declaration. Make it like L1 = ['apples','oranges','bananas','pears']. Its working fine. Commented Nov 25, 2018 at 19:10
  • also paste the full error so we know where it happened Commented Nov 25, 2018 at 19:11

3 Answers 3

3

Create your lists like below:

In [369]: l1 = ['apples','oranges','bananas','pears']
In [357]: l2 = ['apples','tomatoes'] 

And then do the intersection:

In [370]: set(l1).intersection(l2)
Out[370]: {'apples'}
Sign up to request clarification or add additional context in comments.

Comments

2

You are forgetting quotes, in both the lists.

l1 = ['apples', 'oranges', 'bananas', 'pears']
l2 = ['apples','tomatoes']
b2 = set(l1).intersection(set(l2))

To elaborate, what you are comparing, in your example, are the strings 'apples,oranges,bananas,pears', 'apples', 'tomatoes' (with one quote supplied, else it would not compile). Clearly, the intersection of that sets is a null set. That is what you are getting, imo.

Comments

0
TypeError: 'NoneType' object is not iterable

This error occurs when your list is not iterable. That is when you have None object to iterate over it. There are multiple ways to handle the error, however, the best solution is

if not list:
  print('List object is not iterable as it is empty!!')

But in your case, it's a human error, which is not using quotes after the word/sentence ends.

l1 = ['apples','oranges','bananas','pears']  
l2 = ['apples','tomatoes']           
b2 = set(l1).intersection(l2)

This should do the trick. :)

2 Comments

The first two parts are incorrect. In the first part, lists are always iterable, even empty lists (iterating 0 times simply does nothing, but does not throw an error). The problem there is, as per the error message, that the object you are trying to iterate is None, which is clearly not a list or an iterable. In the second part, there's a syntax error: If -> if. And the correct way to check if an object x is a list is if isinstance(x, list).
I do support @Fractalism 's comment tho, Please ensure that you get all the required info from the questioner before proceeding, As it might save effort

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.