OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

For loop through the list unless empty?

  • Thread starter Thread starter Oli
  • Start date Start date
O

Oli

Guest
I've been writing a lot of constructs like this the past couple of days:

Code:
list = get_list()
if list:
    for i in list:
        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)
<p>I've been writing a lot of constructs like this the past couple of days:</p>

<pre><code>list = get_list()
if list:
for i in list:
pass # do something with the list
else:
pass # do something if the list was empty
</code></pre>

<p>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?</p>

<p>(My understanding is that the <code>else</code> in the <code>for: else:</code> construct always triggers after it has looped, empty or not - so not what I want)</p>
 

Latest posts

Top