Reputation: 239790
I've been writing a lot of constructs like this the past couple of days:
things = get_list()
if things:
for i in things:
pass # do something with the list
else:
pass # do something if the list was empty
Lot of junk and I assign the list to a real variable (keeping it in memory longer than needed). Python has simplified a lot of my code up til now... Is there a easy way to do this?
(My understanding is that the else
in the for ... else:
construct always triggers after it has looped, empty or not - so not what I want)
Upvotes: 43
Views: 80954
Reputation:
Use a list comprehension:
def do_something(x):
return x**2
things = []
result = [do_something(x) for x in things]
print result # []
things = [1, 2, 3]
result = [do_something(x) for x in things]
print result # [1, 4, 9]
Upvotes: 11
Reputation: 6748
I get that it's been about 15 years since it was asked, but now Python has the "walrus operator" to do exactly what you asked for with only a slight tweak to your original code:
if my_list := get_list():
for i in my_list:
pass # do something with the element
else:
pass # do something if the list was empty
There's no strong scoping like in other languages tho, so my_list
will still be around after the statement, but you've clearly expressed your intent.
Upvotes: 0
Reputation: 1
i = None
for i in get_list():
pass # do something with the list
else:
if i is None:
pass # do something if the list was empty
Does that help? Yes I know we are two years away from the need :-)
Upvotes: 0
Reputation: 16625
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()
Upvotes: 1
Reputation: 19029
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 []]
Upvotes: 89
Reputation: 18816
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
Upvotes: 4
Reputation: 211924
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 with
ing:
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.
Upvotes: 5
Reputation: 45324
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)
Upvotes: 2