Listの性質を理解する
最終更新日:2021/6/21
【説明すること】
- Listに要素を追加する方法と注意点
li1+li2
,extend()
,append()
リストに要素を追加する方法は a+b
, extend()
, append()
があります。
a+bとextend()は同じ結果に見えますが、全く異なるものです。
a+b
だと、aもbも上書きされません。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1,2] #List
b = [3,4] #List
print('a + b =',a + b)
print('a =',a)
print('b =',b)
#a + b = [1, 2, 3, 4]
#a = [1, 2]
#b = [3, 4]
元のリストをそのまま残し、追加後の結果を新しいリストにしたい場合が多いでしょう。その場合、結果を入れるリスト(受け皿 li1)を用意して”+”を使います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e = [11,12] #List
f = [13,14] #List
li1 = e + f
print('li1=', li1)
#li1= [11, 12, 13, 14]
extend()
だと、元のliが上書きされます。それで良ければ、cを追加後のリストとして使えばいいです。
受け皿はいらない(new_c = c.extend(d)と書かない)。
extend()で受け皿を書くと、思わぬ結果になるので書きません。受け皿が必要なら、上の”a+b”か下の「空のリストを作ってextend()
」と書きます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c = [5,6] #List
d = [7,8] #List
c.extend(d)
print('c=',c)
#c= [5, 6, 7, 8]
append()
だと、list in list(リストの中にリストが出来る)になります。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a.append(b)
print('a=',a)
#a= [1, 2, [3, 4]]
注意が必要なコード
意図と異なったケースを示します。
「li1をli3に代入(li3を新たなリストと考えている)。li1はそのまま残す(追加しない)。その後、li3だけに要素15を追加しよう」としました。
ところが、こう書くと、li1も上書きされます。つまり、li3=li1と書くと、どちらかを操作すると両方影響を受けます。意図した結果か、注意が必要です。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
li3 = li1 # こう書くと、同じものを参照するようになる
print('li1=', li1)
print('li3=', li3)
# li1= [11, 12, 13, 14]
# li3= [11, 12, 13, 14] #li1から複製された、新しいリストに見える
li3.extend([15])
print('li1=', li1)
print('li3=', li3)
# li1= [11, 12, 13, 14, 15] #li1にも15が追加されている。意図通りか?
# li3= [11, 12, 13, 14, 15]
li3 = li1 + li2でもいいのか?
ここで、li3 = li1 + li2
と書いてもいいのか?という疑問が湧きます。結論は問題ありません。
“+”の場合、新たなリストとして認識されるので、上のようにはなりません。li3.extend(li4)
としても、li1とli2に、li4は追加されません。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e = [11,12] #List
f = [13,14] #List
li1 = e + f
print('li1=', li1)
#li1= [11, 12, 13, 14]
li1.extend([91])
print('li1=',li1)
print('e=',e)
print('f=',f)
#li1= [11, 12, 13, 14, 91]
#e= [11, 12] #91は追加されない
#f= [13, 14] #91は追加されない
実際に書いているコード
上記のような混乱や誤解を避ける為に、現場では、”+”ではなく、明示的に、以下のように空のリストを用意してextend()
と書く事があります。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = [21,22]
j = [23,24]
li5 =[] #空のリストを作る
li5.extend(i) #li5にiの.extend()で要素を追加
print('li5(i)=',li5)
#li5(i)= [21, 22]
li5.extend(j) #li5にiの.extend()で要素を追加
print('li5(ij)=',li5)
print('i=',i)
print('j=',j)
#li5(ij)= [21, 22, 23, 24]
#i= [21, 22] #iは元のまま
#j= [23, 24] #jは元のまま
- 追加後の新しいリストは
li3 = li1 + li2
と書く。
または、空のリストを作ってextend()
と書く。 extend()
は要素を追加し、元のリストが上書きされるappend()
はlist in listになり、元のリストが上書きされるli1 = li2
と書くと、どちらかを操作しただけで、両方に影響が出る