python3

python3 で、リストを pop したものの返り値

投稿日:

リストを pop すると、 pop された値が返される。

plant_1 = ['pumpkin', 'ginger', 'potato']
plant_2 = []

print('plant_1 is ' + str(plant_1))
print('plant_2 is ' + str(plant_2))
plant_2.append(plant_1.pop())

print('============')

print('plant_1 is ' + str(plant_1))
print('plant_2 is ' + str(plant_2))

結果

plant_1 is ['pumpkin', 'ginger', 'potato']
plant_2 is []
============
plant_1 is ['pumpkin', 'ginger']
plant_2 is ['potato']

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で目盛りに文字を使用する方法

文字に tex の記法を使って数式を入力することもできる。 例 import numpy as np from matplotlib import pyplot as plt x = np.linsp …

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …

no image

matplotlib で折れ線グラフの下部に色をつけて塗りつぶす方法

pyplot の fill_between を使う。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = & …

no image

python の __str__ と __repr__ とは

python でクラスの情報を文字列で表示するとき、__str__ メソッドと __repr__ メソッドが使える。__str__ は、プログラマーとは限らないユーザーに「読める形で情報を表示」するこ …