Python:从空列表中弹出 [英] Python: pop from empty list

查看:37
本文介绍了Python:从空列表中弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中循环使用下面的行

I am using below line in a loop in my code

importer = exporterslist.pop(0)

如果exporterslist 没有条目或者它是null,它返回error: IndexError: pop from empty list.如何绕过没有条目的 exporterslist?

If exporterslist has no entries or it is null, it returns error: IndexError: pop from empty list. How can I bypass exporterslist with no entries in it?

我能想到的一个想法是,如果 exporterslist 不为 null,则 importer = exporterslist.pop(0)否则获取循环中的下一个条目.如果想法正确,如何在python中编码?

One idea I can think of is if exporterslist is not null then importer = exporterslist.pop(0) else get the next entry in the loop. If the idea is correct, how to code it in python?

推荐答案

您走在正确的轨道上.

if exporterslist: #if empty_list will evaluate as false.
    importer = exporterslist.pop(0)
else:
    #Get next entry? Do something else?

这篇关于Python:从空列表中弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆