I'm completely stumped on a computer science assignment because our lecturer has given us zero information. We're supposed to have the user input a list and check if the list is in decreasing order and print a suitable message if it is. That part works fine. However, if the list is "empty" (I'll explain the quotations in a second) then it should print the list is empty.
The problem is that, according to our brief we're supposed to use .split(",") in order to allow the user to separate their numbers with commas. The problem lies in the fact that even if a user hits enter without inputting anything, the list is still '1' long with just an empty string. I've been scouring the web to find out how to check if a list is empty. But all the tutorials have so far just been in relation to actually empty lists (as in, list = []). One did use the all() function to check that all items in the list were strings, which did work and let me print that the list is empty, but broke the original function. Could someone please help me I'm tearing my hair out with this.
--
def checkOrder():
# checks, etc.
integer_list = []
listLength = len(integer_list)
increasing = 0
decreasing = 0
singleValue = 0
# Converting inputted numbers into a split list
userInput = input("Please enter your numbers seperated by a comma (,):")
inputtedStrings = userInput.split(",")
for number in inputtedStrings:
inputtedIntegers = int(number)
integer_list.append(inputtedIntegers)
# Enumerating list
for i, val in enumerate(integer_list[:-1]):
if val <= integer_list[i+1]:
increasing = 1
elif val >= integer_list[i+1]:
decreasing = 1
# Check for a single value
if len(integer_list) == 1:
singleValue = 1
if len(integer_list) == 1 and
print("The list is empty.")
if increasing == 1:
print("The numbers are in increasing order.")
elif decreasing == 1 or singleValue == 1:
print("The numbers are in decreasing order.")
checkOrder()
--
Videos
Empty strings are "falsy" (python 2 or python 3 reference), which means they are considered false in a Boolean context, so you can just do this:
if not myString:
This is the preferred way if you know that your variable is a string. If your variable could also be some other type then you should use:
if myString == "":
See the documentation on Truth Value Testing for other values that are false in Boolean contexts.
From PEP 8, in the “Programming Recommendations” section:
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
So you should use:
if not some_string:
or:
if some_string:
Just to clarify, sequences are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.
if not a:
print("List is empty")
Using the implicit booleanness of the empty list is quite Pythonic.
The Pythonic way to do it is from the PEP 8 style guide.
For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
# Correct: if not seq: if seq: # Wrong: if len(seq): if not len(seq):
You can just check if there is the element '' in the list:
if '' in lst:
# ...
if '' in lst[:1]: # for your example
# ...
Here is an example:
>>> lst = ['John', '', 'Foo', '0']
>>> lst2 = ['John', 'Bar', '0']
>>> '' in lst
True
>>> '' in lst2
False
any('' in s for s in row[1:])
checks, for each string s in the list row[1:], whether the empty string is a substring of it. That's always true, trivially. What you mean is
any(s == '' for s in row[1:])
But this can be abbreviated to
'' in row[1:]