0

I have a problem:

list = [1,2,3,4,5]
a= 3
if a==[item for item in list]:
    print(sth)

why the program never print? thanks...

1
  • DO NOT name your variable as list,it internally stands for the type of List Commented Jul 5, 2013 at 10:08

1 Answer 1

5

You're comparing an integer to a list, which will never return True as they are different types. Note that [item for item in list] is exactly the same as just saying list.

You're probably wondering if 3 is in the list; so you can do:

if a in list:
    print(sth)

Or even:

if any(a == item for item in list):
    print(sth)

(Although you really should just use the first option. I only put the second option in as it looks similar to your example :p)


As a side note, you shouldn't be naming lists list, or dictionaries dict, as they are built-in types already, and you're just overriding them :p.

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

1 Comment

thanks buddy, im quite stupid now after whole day studying programming. also thank you for replying so fast.

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.