List
- 단일 변수에 여러 항목을 저장하는 데 사용
- 데이터 컬렉션을 저장하는 데 사용되는 Python의 4 가지 내장 데이터 유형 중 하나
- 나머지 3 개는 Tuple, Set, Dictionary
- 순서가 지정되고 변경 가능하며 중복 값을 허용
thislist = ["apple", "banana", "cherry"]
print(thislist)
List 값 변경
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant로 " #banana를 blackcurrant로 변경
print(thislist)
두 번째 값을 두 개의 새 값 으로 대체하여 변경
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)
항목 추가(append)
thislist = ["apple", "banana", "cherry"]
thislist.append("orange") #thislist 리스트에 orange 추가
print(thislist)
지정 위치에 삽입(insert)
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
목록확장(extend)
- Tuple, Set, Dictionary 추가 가능
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
thislist = ["apple", "banana", "cherry"] #list
thistuple = ("kiwi", "orange") # tuple
thislist.extend(thistuple)
print(thislist)
목록제거(remove)
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")#banana 제거
print(thislist)
pop
- 인덱스를 지정하지 않으면 pop()메서드는 마지막 항목을 제거
thislist = ["apple", "banana", "cherry"]
thislist.pop(1) # pop을 이용해 banana 제거
print(thislist)
출처