본문 바로가기
python

list끼리 + 는 되지만 - 는 안된다? / append , extend 차이

by sj0020 2020. 6. 17.
>>> a= [1,2,3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> a.extend([5,6])
>>> a
[1, 2, 3, 4, 5, 6]
>>> a.remove()
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    a.remove()
TypeError: remove() takes exactly one argument (0 given)
>>> 
>>> a.remove(4)
>>> a
[1, 2, 3, 5, 6]
>>> b =[7,8]
>>> a + b
[1, 2, 3, 5, 6, 7, 8]
>>> a.insert(3,4)
>>> a
[1, 2, 3, 4, 5, 6]
>>> a= a+ b
>>> a
[1, 2, 3, 4, 5, 6, 7, 8]
>>> a= a-b
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    a= a-b
TypeError: unsupported operand type(s) for -: 'list' and 'list'

append: adds a single element

extend: merges a list onto another. extend(x) 에서 x 는 리스트!!만 올 수 있음

a.extend([4, 5]) 는  a +=[4, 5]

'python' 카테고리의 다른 글

#positional formationg, #named placeholder  (0) 2020.07.12
"pithon" 을 "python"으로 바꾸기  (0) 2020.06.16
sequence of "non-decreasing" numbers  (0) 2020.06.12
print odd numbers (1 ~199)  (0) 2020.06.12