Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For list unless empty in python

People also ask

How do I check if a list is empty in Python?

If the length of a list is 0, then the list is empty.

How do I use isEmpty in Python?

In this solution, we use the len() to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.

Is an empty list none in Python?

Usually, an empty list has a different meaning than None ; None means no value while an empty list means zero values.

Can you have an empty list in Python?

You can create an empty list using an empty pair of square brackets [] or the type constructor list() , a built-in function that creates an empty list when no arguments are passed. Square brackets [] are commonly used in Python to create empty lists because it is faster and more concise.


Based on the other answers, I think the cleanest solutions are

#Handles None return from get_list
for item in get_list() or []: 
    pass #do something

or the comprehension equiv

result = [item*item for item in get_list() or []]

Use a list comprehension:

def do_something(x):
  return x**2

list = []
result = [do_something(x) for x in list if list]
print result        # []

list = [1, 2, 3]
result = [do_something(x) for x in list if list]
print result       # [1, 4, 9]

Slighty more terse is:

for i in my_list:
    # got a list
if not my_list:
    # not a list

assuming you are not changing the length of the list in the loop.

Edit from Oli: To compensate my worries of memory use, it would want withing:

with get_list() as my_list:
    for i in my_list:
        # got a list
    if not my_list:
        # not a list

But yes, that's quite a simple way around the issue.


If your actions are different, I would do:

list_ = get_list() # underscore to keep built-in list
if not list_:
    # do something
for i in list_: #
    # do something for each item

If your actions are similar, this is more beautiful:

for i in list_ or [None]:
   # do something for list item or None

or, if you might have None as a list element,

for i in list_ or [...]:
   # do something for list item or built-in constant Ellipsis

def do_something_with_maybe_list(maybe_list):
    if maybe_list:
        for x in list:
            do_something(x)
    else:
        do_something_else()

do_something_with_maybe_list(get_list())

You could even extract the actions to be done:

def do_something_with_maybe_list(maybe_list, process_item, none_action):
    if maybe_list:
        for x in list:
            process_item(x)
    else:
        none_action()

do_something_with_maybe_list(get_list(), do_something, do_something_else)
do_something_with_maybe_list(get_otherlist(), do_other, do_still_other)

Edit from Oli: Or go one further:

def do_something_with_maybe_list(maybe_list, process_item, none_action):
    if maybe_list:
        return process_list(maybe_list)
    return none_action()

do_something_with_maybe_list(get_list(), do_something, do_something_else)
do_something_with_maybe_list(get_otherlist(), do_other, do_still_other)

I think your way is ok in general case, but you may consider this approach:

def do_something(item):
   pass # do something with the list

def action_when_empty():
   pass # do something if the list was empty

# and here goes your example
yourlist = get_list() or []
another_list = [do_something(x) for x in yourlist] or action_when_empty()