3

Here's an excerpt from my code!

division = ["Division","Divide","/","div"]
multiplication =["*","x","times","multiply","multiplication","multiple"]
subtraction = ["-",'minus','subtract','subtraction']
addition = ['+','plus','addition','add']
root = ['root','squareroot','square root']
square = ['square','squared','power 2']

choice = input('calculation type')
print(choice == (division or multiplication or subtraction or addition))

So far, it only gives "False". How can I check if a variable exists in multiple lists? I've tried to make lists inside of lists but I still get "False", Here's the code of that...

division = ["Division","Divide","/","div"]
multiplication = ["*","x","times","multiply","multiplication","multiple"]
subtraction = ["-",'minus','subtract','subtraction']
addition = ['+','plus','addition','add']
root = ['root','squareroot','square root']
square = ['square','squared','power 2']
basic_double = [division,multiplication,subtraction,addition]
basic_single = [root,square]
choice = input('calculation type')
print(choice == basic_double or basic_single)

Any help would be appreciated! :D thank you!!!

5
  • any(choice in ls for ls in [division, multiplication, subtraction, addition]) Commented Nov 15, 2016 at 23:14
  • @L3viathan I hope its not too much, I'm really new xD, but can you please explain HAHAHHAHA SORRY :DD Commented Nov 15, 2016 at 23:15
  • I will explain in the answer. Commented Nov 15, 2016 at 23:15
  • I feel like there should be a list of common mess-up sources and the reasons why they are wrong. Lots of people seem to have trouble with the fact that or in python doesn't work like that. Commented Nov 15, 2016 at 23:18
  • @ElliotRoberts that would be really helpful for beginners 😂 Commented Nov 15, 2016 at 23:29

5 Answers 5

3

Test whether the choice is in any of the lists:

any(choice in ls for ls in [division, multiplication, subtraction, addition])

any returns True if at least one of the elements of the given iterable are truthy.

choice in ls tests whether choice is an element of the list.

choice in ls for ls in [division, multiplication, subtraction, addition] is a generator comprehension, meaning it's an iterator that returns the result of choice in ls for any possible ls in [division, multiplication, subtraction, addition].

If one of those lists contains the choice, any will return True, False otherwise.

Sign up to request clarification or add additional context in comments.

Comments

3

You may take the use of itertools.chain() to check the choice in all the list as:

if choice in chain(division, multiplication, subtraction, addition):

Comments

1

If you want to check if choise is in any of the four lists you provided in your example:

if choice in division + multiplication + subtraction + addition:
   # do something when its in any of it.
   ...

Comments

0

Right now, you're comparing a String, choice, to a series of lists. This will never be True as you're comparing two different data types.

You should check against each list using the "in" keyword

if choice in division:
     #this returns true if the String set to choice is in the list of objects in division.
else if choice in multiplication:
.
.
.

This comparison will help you find the word in the list that you're looking for.

Comments

0

Right now, your code is doing the following comparison inside the print function:

choice == ["Division","Divide","/","div"] or ["*","x","times","multiply","multiplication","multiple"] or ["-",'minus','subtract','subtraction'] or ['+','plus','addition','add']

Because of short-circuiting, this reduces to:

choice == ["Division","Divide","/","div"]

Which always evaluates to False because choice is not a list.

I'd flatten the list containing [division, multiplication, subtraction, addition] and then test to see if choice is in that list.

flattened = [item for ls in [division, multiplication, subtraction, addition] for item in ls]

print(choice in flattened)

See this answer to this question for more details on flattening a list of lists in python.

Comments

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.