Python Forum
what to return for an empty list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what to return for an empty list
#1
i have a function to test if multiple items all meet a certain condition. another function is to test if multiple items all fail to meet a certain condition. two more functions do these tests if any item (one or more) meets, or fails to meet, a certain condition. what the condition is, is fixed. for a different condition, there would be four more functions. the items are passed in a list or tuple. the return value is a boolean, True or False. if any item type does not make sense (such as passing an int when the condition being tested is if a character is an English vowel) an exception is raised (such as TypeError).

if the passed list (or tuple) is empty, should this function raise an exception or return some value? which exception? there seems to be no EmptyListError. i'd like to be sure any exception raised applies only one way or the other so the nature of the error is clear.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(May-24-2024, 03:38 AM)Skaperen Wrote: if the passed list (or tuple) is empty, should this function raise an exception or return some value?
The best is the rule from mathematical logic. If «any of them meets Condition», it implies that the list is not empty. Consequently, «any» should return False when the list is empty. The opposite statement should return True when the list is empty, but the opposite statement is «all of them meet not Condition». Consequently «all» must return True when the list is empty.
>>> any([])
False
>>> all([])
True
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
i'm expanding these functions to also accept multiple items as individual arguments. that way the call could be done like fun([foo,bar]) or like fun(foo,bar). the functions any() and all() raise TypeError if no arguments are given. but this expanding means i need to treat fun() like fun([]) which is doable. it just doesn't have the concise analogy of any() and all().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 16,125 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Code with empty list not executing adeana 9 3,957 Dec-11-2023, 08:27 AM
Last Post: buran
  set.difference of two list gives empty result wardancer84 4 1,637 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  displaying empty list vlearner 5 1,806 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove empty keys in a python list python_student 7 3,315 Jan-12-2022, 10:23 PM
Last Post: python_student
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,260 Jan-09-2022, 02:39 AM
Last Post: Python84
  How to invoke a function with return statement in list comprehension? maiya 4 3,033 Jul-17-2021, 04:30 PM
Last Post: maiya
  Regular expression: return string, not list Pavel_47 3 2,616 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  What is the value after JOINING an empty list? JaneTan 2 5,482 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Printing empty list? hhydration 2 2,211 Oct-28-2020, 11:34 AM
Last Post: Atekka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020