0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonのlist処理でちょっと使いづらい点 pop()

Last updated at Posted at 2024-08-30

プログラミングの際に思ったこと

pythonのpopがちょっと使いづらい
例えば、
l=Noneまたはl=[]のとき、

i=l.pop()

としたら、pythonはエラーを返すが、無視して、iにNoneを返して、lはそのままにしていて欲しい。
実装すると、こう

>>> po=lambda l:l.pop() if l else None
>>> l=['a','b']
>>> i=po(l)
>>> l
['a']
>>> i
'b'
>>> l=[]
>>> i=po(l)
>>> l
[]
>>> i
>>> 
>>> l=None
>>> i=po(l)
>>> l
>>> i
>>>

NULLの処理の仕方の問題ですにゃ。

0
0
10

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?