다수의 데이터를 저장하기 위해서는 다수의 변수가 필요함
e.g. 학생 10명의 점수
score0 = 80
score1 = 90
score2 = 87
score3 = 60
score4 = 87
score5 = 91
score6 = 88
score7 = 94
score8 = 83
score9 = 98
만약 학생 수가 10,000명이라면, 10,000개의 변수가 필요함
다수의 값을 하나의 변수로 관리할 수 있는 방법이 필요함
리스트는 여러 개의 자료(요소)들을 모아서 하나의 묶음으로 저장할 수 있는 자료구조임
다수의 값을 하나의 변수에 담아 관리할 수 있음
많은 변수가 필요한 프로그램 작성시 유용함 (e.g., 학생 10,000명의 성적 저장)
[] 로 리스트 생성
리스트의 요소로 리스트가 저장될 수도 있음(중첩 리스트)
리스트의 개별 요소(element)에는 index로 접근할 수 있음(0 부터 시작)
index 범위를 벗어나면 에러 발생(IndexError: list index out of range)

scoreList = [90, 70, 50]
print(scoreList) # [90, 70, 50]
subList = ['kor', 'eng', 'math']
print(subList) # ['kor', 'eng', 'math']
complexList = [['kor', 90], ['eng', 70], ['math', 50]]
print(complexList) # [['kor', 90], ['eng', 70], ['math', 50]]
a = list(range(10)) # begin==10
print(a) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = list(range(5,10)) # begin==5, end==10
print(b) # [5, 6, 7, 8, 9]
c = list(range(1, 10, 2)) # begin==1, end==10, step==2
print(c) # [1, 3, 5, 7, 9]
d = list(range(10, 0, -1))
print(d) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
scoreList = [90, 70, 50]
print(scoreList[0]) # 90
print(scoreList[-1]) # 50
print(scoreList[3])
index 범위를 벗어나 에러 발생
complexList = [['kor', 90], ['eng', 70], ['math', 50]]
print(complexList[1]) # ['eng', 70]
print(complexList[1][0]) # eng
print(complexList[1][1]) # 70
scoreList = [90, 70, 50, 60, 80, 100, 95]
scoreList[2:4] # [50, 60], [begin:end] -> begin ~ end-1
scoreList[:4] # [90, 70, 50, 60], [:end] -> 0 ~ end-1
scoreList[5:] # [100, 95], [begin:] -> 5 ~ end
scoreList[:] # [90, 70, 50, 60, 80, 100, 95] -> 0 ~ end
scoreList = [90, 70, 50, 60, 80, 100, 95]
scoreList[::2] # [90, 50, 80, 95], [::step]
scoreList[3::2] # [60, 100], [begin::step]
scoreList[3::-1] # [60, 50, 70, 90], [begin::-step]
scoreList[3:0:-1] # [60, 50, 70, 90], [begin:end:-step]
scoreList[::-1] # [95, 100, 80, 60, 50, 70, 90]
scoreList = [90, 50, 80, 95]
print(scoreList)
90, 50, 80, 95 4개의 데이터를 하나의 리스트로 패킹
test = scoreList
kor, eng, math, art = scoreList
print(kor) # 90
scoreList의 요소 4개를 각 변수로 언패킹
언패킹할 때 리스트 요소의 수와 변수의 수가 일치해야 함
kor, eng, math, art, sci = scoreList # 변수가 더 많을 경우 Error
kor, eng, math = scoreList # List 요소의 수가 더 많은 경우 Error
scoreList = [90, 70, 50, 60, 80, 100, 95]
len(scoreList) # 7
scoreList.append(40)
scoreList # [90, 70, 50, 60, 80, 100, 95, 40]
리스트의 마지막에 40이 추가되었음
scoreList # [90, 70, 50, 60, 80, 100, 95, 40]
scoreList.insert(2, 99)
scoreList # [90, 70, 99, 50, 60, 80, 100, 95, 40]
numList1 = [70, 60, 50]
numList2 = [100, 200, 300]
numList3 = numList1 + numList2
print(numList3) # [70, 60, 50, 100, 200, 300]
print(numList1) # [70, 60, 50]
print(numList2) # [100, 200, 300]
numList3 = numList1.extend(numList2)
numList3 # None
numList1 # [70, 60, 50, 100, 200, 300]
scoreList = [100, 70, 50, 60, 80, 100, 95, 40]
scoreList.remove(100) # 요소 중 값이 100인 요소 삭제
scoreList # [70, 50, 60, 80, 100, 95, 40]
del(scoreList[0]) # index가 0인 요소 삭제
scoreList #[50, 60, 80, 100, 95, 40]
del(scoreList)
print(scoreList)
scoreList 자체를 삭제하였으므로, print() 함수를 이용하여 출력 시 에러 발생
scoreList = [90, 70, 50, 60, 80, 100, 95]
del(scoreList[2:4]) # slice를 사용하여 여러개를 동시에 삭제 2 ~ 4-1
print(scoreList) # [90, 70, 80, 100, 95]
del(scoreList[:]) # 내부 요소 전체 삭제
scoreList # []
del(scoreList)와는 다르게 마지막 줄에서 에러가 나지 않음
(리스트는 남겨두고 내부 요소만 삭제하였기 때문)
scoreList.clear()
scoreList # []
list 원형이 정렬 결과에 따라 변형됨
안정성이 보장됨
- 안정성: 같은 값을 가진 요소들의 순서가 정렬 이후에도 유지되는 것
- 불안정 정렬 알고리즘의 대표적인 예 - 선택 정렬: 3, 5, 9, 7, 10, 9, 6
scoreList = [90, 70, 50, 60, 80, 100, 95]
scoreList.sort() # default: 오름차순
print(scoreList) # [50, 60, 70, 80, 90, 95, 100]
scoreList.sort(reverse=True) # 내림차순
scoreList # [100, 95, 90, 80, 70, 60, 50]
scoreList = [90, 70, 50, 60, 80, 100, 95]
sortedList = sorted(scoreList) # [50, 60, 70, 80, 90, 95, 100]
print(scoreList) # [90, 70, 50, 60, 80, 100, 95]
print(sortedList) # [50, 60, 70, 80, 90, 95, 100]
stringList = ['google', 'go', 'goo']
stringList.sort(key=len) # 길이순으로 정렬
print(stringList) # ['go', 'goo', 'google']
complexList = [['kor', 90], ['eng', 70], ['math', 50]]
newList = sorted(complexList, key=lambda x:x[1]) # [['math', 50], ['eng', 70], ['kor', 90]], 각 요소의 1번 요소인 점수 순으로 정렬
print(newList) # [['math', 50], ['eng', 70], ['kor', 90]]
scoreList = [90, 70, 50, 60, 80, 100, 95]
scoreList.reverse()
scoreList # [95, 100, 80, 60, 50, 70, 90]
scoreList = [90, 70, 50, 60, 80, 100, 100]
scoreList.index(100) # 5, value가 100인 요소의 index 반환
scoreList.count(100) # 2, value가 100인 요소의 개수 반환
60 in scoreList # True
999 in scoreList # False
60 not in scoreList # False
999 not in scoreList # True
total = sum(scoreList) # 550, 전체 요소의 합계 계산 후 반환
print(total)
minVal = min(scoreList)
maxVal = max(scoreList)
print(minVal) # 50
print(maxVal) # 100