본문 바로가기
Python 배우기

list_items.py

by 노화방지 Anti-aging Hairstyle 2016. 3. 7.
반응형
animals = ['elephant', 'lion', 'tiger', "giraffe", "monkey", 'dog']   # create new list
print(animals)

animals[1:3] = ['cat'] # replace 2 items -- 'lion' and 'tiger' with one item -- 'cat'
print(animals)

animals[1:3] = [] # remove 2 items -- 'cat' and 'giraffe' from the list
print(animals)

animals[0:4] = []
print(animals)

산출물 (Output)
['elephant', 'lion', 'tiger', 'giraffe', 'monkey', 'dog']
['elephant', 'cat', 'giraffe', 'monkey', 'dog']
['elephant', 'monkey', 'dog']
[]


* slices에 대한 할당도 또한 가능하며, 이것은 list의 크기도 변경시키거나
완전히 clear 할 수 있습니다.
<br><br>
Clear "animals" list.
<br>


반응형

'Python 배우기' 카테고리의 다른 글

dicts.py  (0) 2016.03.07
tuples.py  (0) 2016.03.07
list_operations.py  (0) 2016.03.07
lists.py  (0) 2016.03.06
string_methods.py  (0) 2016.03.06

댓글