python list item 추가

오픈소스·2020년 10월 18일
0

https://medium.com/broken-window/many-names-one-memory-address-122f78734cb6
https://stackoverflow.com/questions/725782/in-python-what-is-the-difference-between-append-and

+=

__iadd__()
>>> my_list = [1, 2, 3]
>>> id(my_list)
140418271352128
>>> another_list = my_list
>>> id(another_list)
140418271352128
>>> another_list += [4,]
>>> id(another_list)
140418271352128
>>> print(another_list)
[1, 2, 3, 4]
>>> print(my_list)
[1, 2, 3, 4]

= +

__add__()

This function returns a new copy instead of modifying the list in place.

>>> my_list = [1, 2, 3]
>>> id(my_list)
140418271454720
>>> another_list = my_list
>>> id(another_list)
140418271454720
>>> another_list = another_list + [4,]
>>> id(another_list)
140418271504832
>>> print(another_list)
[1, 2, 3, 4]
>>> print(my_list)
[1, 2, 3]

append()

>>> my_list = [1, 2, 3]
>>> id(my_list)
140418271352128
>>> another_list = my_list
>>> id(another_list)
140418271352128
>>> another_list.append(4)
>>> id(another_list)
140418271352128
>>> print(another_list)
[1, 2, 3, 4]
>>> print(my_list)
[1, 2, 3, 4]

extend()

>>> my_list = [1, 2, 3]
>>> id(my_list)
140418271504832
>>> another_list = my_list
>>> id(another_list)
140418271504832
>>> another_list.extend([4])
>>> id(another_list)
140418271504832
>>> print(another_list)
[1, 2, 3, 4]
>>> print(my_list)
[1, 2, 3, 4]

0개의 댓글