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 で、縦横の罫線を引く方法

破線をカスタマイズする方法 以下では、dashes = [5,2,2,2] で、線5・空白2・線2・空白2の破線を指定している。 詳しくは次を参照。 https://matplotlib.org/st …

no image

python3 でファイルに内容を追記する方法

ファイルを開くとき、「a+」を指定することで、data.txt というファイルを書き込みモードで開く。(もしファイルが存在していなければ作成する。) 例 f = open(‘data.txt’, ‘a …

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …

no image

matplotlib で、凡例を表示する方法

データに label を関連付け、plt.legend() で凡例を表示する。 例 from matplotlib import pyplot as plt data_x = [25,26,2 …

no image

matplotlib で、グラフの線の幅を変更する方法

lw で、線幅を数字で指定すればよい。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [39, …