0

I am learning to create a block of 'if' statements to check certain conditions and I want them to identify if the list has empty quote marks in it.

so for example:

favourite_fruit = []

if len(favourite_fruits) == 0:
    print('Fruits are an important part of one's diet')
###This code works which is great as long as the list is empty in terms of it's length.

if the list isn't empty and they have something like: 'apple' in it, but another fruit like 'dates' is missing, then I would tell it to say:

if 'dates' not in favourite_fruit and len(favourite_fruit) != 0:
    print ('Have you tried Dates? They are high in fibre, a good source of which, can help prevent constipation by promoting bowel movements.') 

But the problem is if I type:

favourite_fruit = ['']

The length of this list is 1 but there is nothing in it so it will print out the dates quote but not the 'fruits are important' quote.

Is there a way for me to get python to identify there is nothing actually written in the list?

I am pretty much a beginner so I am still learning

Here is what I have tried though:

favourite_fruit = ['']

if 'dates' not in favourite_fruit and len(favourite_fruit) != 0 and favourite_fruit != "" and favourite_fruit != "\"\"" and favourite_fruit != '' and favourite_fruit != '\'\'':
    print ('Have you tried Dates? They are high in fibre, a good source of which, can help prevent constipation by promoting bowel movements.')

But it still isn't working.

3
  • 8
    Why are you putting empty strings in the list in the first place? Commented Oct 1, 2021 at 20:08
  • favourite_fruit != [''] Commented Oct 1, 2021 at 20:10
  • "The length of this list is 1 but there is nothing in it " yes, there is something in it. a str object. Hence why it's length is not 0 Commented Oct 1, 2021 at 20:42

2 Answers 2

1

Wondering why there are empty strings in the list.

Anyway, assuming you want to ignore empty strings, you may filter your list first. In Python, this is typically achieved with the following syntax, which is called a list comprehension:

favourite_fruit = [f for f in favourite_fruit is f != ""]

[''] becomes [] (empty list), ['apple', ''] becomes ['apple'], etc. You get the idea.

Sidenote: in Python, a non-empty list is truthy and an empty list is falsy, so if len(favourite_fruits) == 0 can be written if favourite_fruits.

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

4 Comments

I think you mean if favourite_fruit.
Also, favourite_fruit = [''] is never treated as an empty list. By running that list comprehension, you overwrite the original list with an actually empty list.
Absolutely. That's the point. But the explanation is unclear. Let me rephrase.
"I think you mean if favourite_fruit." Indeed. Edition rush.
0

Is there a way for me to get python to identify there is nothing actually written in the list?

Empty strings are false, so all you need to do is check if any of the items in favourite_fruit is true.

any([])         => False
any([''])       => False
any(['dates'])  => True

8 Comments

@Pranav Thanks, I guess, though please don't make it look like I use those abominable words.
Falsy/truthy is not the same as false/true though. I removed my upvote for incorrect information. Those are actual terms, not something I made up google.com/search?q=falsy+truthy stackoverflow.com/q/39983695/843953
@PranavHosangadi You're the one with incorrect information. true/false are the correct terms in Python terminology, check the documentation for example here or at any other place where it talks about boolean values. It is the same thing people like you mean when they say falsy/truthy, just the correct terminology in Python.
I second @PranavHosangadi. Falsy is commonly used to remove the ambiguity between bool(a) is False and a is False. Saying that [] is False is ambiguous.
An object being considered True is not the same as it actually being True. For example, '' == False gives False. Empty sequences are not False, they are considered False, which is the whole reason terms like truthy/falsy came about, and "empty strings are false" can be misleading.
|

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.