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 で、線の種類を変更する方法

「linestyle = 」として、線の種類を指定することができる設定の詳細は、次のリンクを参照。 https://matplotlib.org/stable/gallery/lines_bars_a …

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …

no image

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

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

no image

python3 でコンソールからの入力をリストに格納する方法

入力を受け取るには、input() を使う。range(0,5) で、5個の文字列を順に受け取り、リストに入れてそれを表示する。 例 list1 = [] for i in range(0, …

no image

python でファイルを1行おきに読み込む方法

readline を使って次のように書く。 例 ファイル:data.txt # id name age 1 佐藤太郎 10 2 鈴木花子 18 3 坂本明美 21 4 松村光子 24 5 小川奏子 1 …