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.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Riaz01
  • 1

2 Answers2

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.

Jérôme
  • 13,328
  • 7
  • 56
  • 106
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
no comment
  • 6,381
  • 4
  • 12
  • 30
  • @Pranav Thanks, I guess, though please don't make it look like I use those abominable words. – no comment Oct 01 '21 at 22:11
  • 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 https://www.google.com/search?q=falsy+truthy https://stackoverflow.com/q/39983695/843953 – Pranav Hosangadi Oct 02 '21 at 13:42
  • @PranavHosangadi You're the one with incorrect information. true/false are the correct terms in Python terminology, check the documentation for example [here](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) 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. – no comment Oct 02 '21 at 13:57
  • 1
    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. – Jérôme Oct 02 '21 at 17:04
  • 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. – Pranav Hosangadi Oct 02 '21 at 17:05
  • @PranavHosangadi I didn't say `True`/`False`, I said true/false. Don't disregard case and style. And yes, that particular section I linked to unfortunately happens to add the word "considered", but for example the very next section says "if x is false, then". Not "is considered", but simply "is". Other places, too. – no comment Oct 02 '21 at 17:10
  • @Jérôme To you as well: I didn't say `True`/`False`, I said true/false. Don't disregard case and style. I'm exactly following the official Python documentation/terminology. – no comment Oct 02 '21 at 17:12
  • @PranavHosangadi I guess that section says "considered" because it's pretty much the place where the terms are defined. Btw empty sequences are *not* considered `False`, just considered false. At best you could say that like *"empty sequences are considered equivalent to `False` for truth value testing"*. – no comment Oct 02 '21 at 21:18