LoginSignup
0
1

More than 3 years have passed since last update.

リストのループ内で要素の書き換え(Python)

Last updated at Posted at 2020-04-14

こんにちは。
Python で、リストのループ内で、下記のように a = [-1] では、元のリストを書き換えませんが、a[0] = -1 では書き換えます。

alist = [[0], [0], [0]]
for a in alist:
  a = [-1]

print(alist)  # ==>  [[0], [0], [0]]

for a in alist:
  a[0] = -1

print(alist)  # ==>  [[-1], [-1], [-1]]
$ python3 -V
Python 3.7.7
0
1
0

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
1