1. 리스트의 형태
colors = ["red", "blue", "green", "white", "black"]
2. 리스트 인덱싱 및 슬라이싱
colors = ["red", "blue", "green", "white", "black"]
print(colors[3])
=> white
print(colors[2:4])
=> ['green', 'white']
3. 리스트에 원소 추가
colors = ["red", "blue", "green", "white", "black"]
colors.append("yellow")
print(colos)
=> ['red', 'blue', 'green', 'white', 'black', 'yellow']
shapes = []
shapes.append("circle")
print(shapes)
=> ['circle']
4. 리스트의 원소 수정
colors = ["red", "blue", "green", "white", "black"]
print(colors[2])
=> green
colors[2] = "purple"
print(colors[2])
=> purple
5. 리스트 중간에 삽입
colors = ["red", "blue", "green", "white", "black"]
colors.insert(1, "orange")
print(colors)
=>['red', 'orange', 'blue', 'green', 'white', 'black']
6. 리스트의 원소 삭제
colors = ["red", "blue", "green", "white", "black"]
del colors[1]
print(colors)
=>['red', 'green', 'white', 'black']
7. 두 리스트의 결합
colors = ["red", "blue"]
shapes = ["circle", "square"]
colors.extend(shapes)
print(colors)
=> ['red', 'blue', 'circle', 'square']
colors = ["red", "blue"]
shapes = ["circle", "square"]
colorandShape = colors + shapes
print(colorandShape)
=> ['red', 'blue', 'circle', 'square']
8. 리스트의 정렬(숫자 뿐 아니라 글자도 알파벳 순서대로 오름차순 정렬)
number = [5, 3, 8, 6, 7, 2]
number1 = sorted(number)
colors = ["red", "blue", "green", "white", "black"]
colors1 = sorted(colors)
과일 = ["사과", "배", "감", "포도", "수박"]
과일1 = sorted(과일)
print(number1)
=> [2, 3, 5, 6, 7, 8]
print(colors1)
=> ['black', 'blue', 'green', 'red', 'white']
print(과일1)
=> ['감', '배', '사과', '수박', '포도']
9. 리스트의 정렬(내림차순)
number = [5, 3, 8, 6, 7, 2]
number1 = sorted(number, reverse=True)
print(number1)
=> [8, 7, 6, 5, 3, 2]
10. 인덱스 찾기
colors = ["red", "blue", "green", "white", "black"]
print(colors.index("green"))
=> 2