ํ์ด์ฌ์ **๋ฆฌ์คํธ(list)**๋ ์ด๋ณด์์๊ฒ ๊ฐ์ฅ ์ค์ํ ์๋ฃํ ์ค ํ๋์ ๋๋ค.
์๋์ ์ฝ๊ฒ, ์์ ๋ก ํ๋ถํ๊ฒ, ๋จ๊ณ๋ณ๋ก ์ค๋ช ํด๋๋ฆด๊ฒ์. ๊ฐ ์์ ๋ ๋ฐ๋ก ๋ณต์ฌํด์ ์คํํด๋ณผ ์ ์์ต๋๋ค.
1) ๋ฆฌ์คํธ๋?
- ์ฌ๋ฌ ๊ฐ์ ์์ ์๊ฒ ๋ด๋ ๊ทธ๋ฆ(์ปฌ๋ ์ )์ ๋๋ค.
- ๋๊ดํธ
[]๋ก ๋ง๋ค๊ณ , ๊ฐ๋ค์ ์ผํ๋ก ๊ตฌ๋ถํฉ๋๋ค. - ๊ฐ์ ์ข ๋ฅ(์ซ์, ๋ฌธ์, ๋ค๋ฅธ ๋ฆฌ์คํธ ๋ฑ)๋ฅผ ํผํฉํด์ ๋ฃ์ ์ ์์ต๋๋ค.
# ๋ฆฌ์คํธ ์์
nums = [10, 20, 30]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "two", 3.0, True]
nested = [1, [2, 3], ["a", "b"]]
2) ๋ง๋ค๊ธฐ & ๊ธธ์ด ํ์ธ
empty = [] # ๋น ๋ฆฌ์คํธ
data = list("abc") # ['a', 'b', 'c'] ๋ฌธ์์ด์ ๋ฆฌ์คํธ๋ก ๋ณํ
length = len([1, 2, 3, 4]) # 4
print(empty) # []
print(data) # ['a', 'b', 'c']
print(length) # 4
3) ์ธ๋ฑ์ฑ & ์ฌ๋ผ์ด์ฑ
- ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์
- ์ฌ๋ผ์ด์ฑ์
[start:end:step](end๋ ํฌํจ๋์ง ์์)
arr = [10, 20, 30, 40, 50]
print(arr[0]) # 10
print(arr[-1]) # 50 (๋ค์์ ์ฒซ ๋ฒ์งธ)
print(arr[1:4]) # [20, 30, 40]
print(arr[:3]) # [10, 20, 30]
print(arr[2:]) # [30, 40, 50]
4) ๊ฐ ๋ณ๊ฒฝํ๊ธฐ (๊ฐ๋ณ์ฑ)
๋ฆฌ์คํธ๋ mutable(๊ฐ๋ณ) ํฉ๋๋ค.
a = [1, 2, 3]
a[1] = 999
print(a) # [1, 999, 3]
5) ์์ฃผ ์ฐ๋ ๋ฉ์๋
์ถ๊ฐ/์ฝ์ /์ญ์
lst = [1, 2, 3]
lst.append(4) # ๋ค์ ์ถ๊ฐ: [1, 2, 3, 4]
lst.extend([5, 6]) # ์ฌ๋ฌ ๊ฐ ์ถ๊ฐ: [1, 2, 3, 4, 5, 6]
lst.insert(0, 100) # ํน์ ์์น์ ์ฝ์
: [100, 1, 2, 3, 4, 5, 6]
lst.remove(3) # ๊ฐ 3 ์ฒซ ๋ฒ์งธ๋ง ์ ๊ฑฐ: [100, 1, 2, 4, 5, 6]
popped = lst.pop() # ๋งจ ๋ค ๊บผ๋ด๊ธฐ: popped=6, lst=[100, 1, 2, 4, 5]
popped2 = lst.pop(1) # ์ธ๋ฑ์ค 1 ๊บผ๋ด๊ธฐ: popped2=1, lst=[100, 2, 4, 5]
lst.clear() # ๋ชจ๋ ์ง์ฐ๊ธฐ: []
๊ฒ์/๊ฐ์/์ ๋ ฌ
nums = [5, 1, 5, 3, 2]
print(nums.index(5)) # 0 (์ฒซ ๋ฒ์งธ 5์ ์์น)
print(nums.count(5)) # 2 (5์ ๊ฐ์)
nums.sort() # ์ค๋ฆ์ฐจ์ ์ ๋ ฌ: [1, 2, 3, 5, 5]
nums.sort(reverse=True) # ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ: [5, 5, 3, 2, 1]
nums2 = [3, 1, 2]
print(sorted(nums2)) # [1, 2, 3] (์๋ณธ ์ ์ง)
print(sorted(nums2, reverse=True))# [3, 2, 1]
6) ๋ฆฌ์คํธ ๊ฒฐํฉ/๋ณต์
a = [1, 2]
b = [3, 4]
c = a + b # [1, 2, 3, 4]
repeat = ["hi"] * 3 # ['hi', 'hi', 'hi']
7) ๋ฉค๋ฒ์ญ ํ์ธ (in, not in)
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True
8) ๋ฐ๋ณต๋ฌธ๊ณผ ํจ๊ป ์ฐ๊ธฐ
items = ["a", "b", "c"]
for item in items:
print(item)
# a
# b
# c
# ์ธ๋ฑ์ค์ ๊ฐ ๋์์
for i, v in enumerate(items):
print(i, v)
# 0 a
# 1 b
# 2 c
9) ๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์ (์งง๊ณ ๊ฐ๋ ฅ!)
# 0~9์ ์ ๊ณฑ ๋ฆฌ์คํธ
squares = [x2 for x in range(10)]
# ํ์๋ง
odds = [x for x in range(10) if x % 2 == 1]
# ๋ฌธ์์ด ๊ธธ์ด
words = ["hi", "python", "list"]
lengths = [len(w) for w in words]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(odds) # [1, 3, 5, 7, 9]
print(lengths) # [2, 6, 4]
10) 2์ฐจ์ ๋ฆฌ์คํธ (ํ๋ ฌ/ํ ํํ)
# 3x3 ํ๋ ฌ
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
# ํ/์ด ์ ๊ทผ
print(matrix[0]) # [1, 2, 3]
print(matrix[1][2]) # 6
# ๋ชจ๋ ์์ ์ถ๋ ฅ
for row in matrix:
for val in row:
print(val, end=" ")
# 1 2 3 4 5 6 7 8 9
11) ํํ ํจํด ์์ ๋ชจ์
(1) ์ค๋ณต ์ ๊ฑฐ (๊ธฐ๋ณธ)
nums = [1, 2, 2, 3, 3, 3]
unique = []
for n in nums:
if n not in unique:
unique.append(n)
print(unique) # [1, 2, 3]
(2) ์ต๋/์ต์/ํฉ/ํ๊ท
nums = [10, 20, 30, 40]
print(max(nums)) # 40
print(min(nums)) # 10
print(sum(nums)) # 100
print(sum(nums) / len(nums)) # 25.0
(3) ํํฐ๋ง (์กฐ๊ฑด์ ๋ง๋ ๊ฒ๋ง)
nums = [3, 10, 7, 20, 5]
evens = [n for n in nums if n % 2 == 0] # ์ง์๋ง
greater10 = [n for n in nums if n > 10] # 10 ์ด๊ณผ
print(evens) # [10, 20]
print(greater10) # [20]
(4) ๋ฌธ์์ด ๋ฆฌ์คํธ ์ฒ๋ฆฌ
words = [" apple ", "banana", " CHERRY "]
clean = [w.strip().lower() for w in words]
print(clean) # ['apple', 'banana', 'cherry']
(5) ๋ฆฌ์คํธ ํํํ (์ค์ฒฉ ํ๊ธฐ - 1๋จ๊ณ)
nested = [[1, 2], [3, 4], [5]]
flat = []
for group in nested:
for x in group:
flat.append(x)
print(flat) # [1, 2, 3, 4, 5]
(6) ์ ๋ ฌ ๊ธฐ์ค ์ปค์คํฐ๋ง์ด์ฆ (key=)
words = ["apple", "banana", "kiwi", "grape"]
# ๊ธธ์ด ๊ธฐ์ค ์ค๋ฆ์ฐจ์
words.sort(key=len)
print(words) # ['kiwi', 'grape', 'apple', 'banana']
(7) ๋ถํ /๊ทธ๋ฃนํ(์ฒญํฌ)
data = [1,2,3,4,5,6,7,8,9]
size = 3
chunks = [data[i:i+size] for i in range(0, len(data), size)]
print(chunks) # [[1,2,3],[4,5,6],[7,8,9]]
(8) ์ค๋ณต ์นด์ดํธ (๊ฐ๋จ ๋ฒ์ )
items = ["a", "b", "a", "c", "b", "a"]
counts = {}
for x in items:
counts[x] = counts.get(x, 0) + 1
print(counts) # {'a': 3, 'b': 2, 'c': 1}
12) ์์ฃผ ๊ฒช๋ ์ค์ & ํ
(1) ์ฌ๋ผ์ด์ฑ์ ๋ณต์ฌ๋ณธ์ ๋ง๋ญ๋๋ค
a = [1, 2, 3]
b = a[:] # ๋ณต์ฌ
b[0] = 999
print(a, b) # [1, 2, 3] [999, 2, 3]
(2) ์์ ๋ณต์ฌ vs ๊น์ ๋ณต์ฌ
import copy
a = [[1, 2], [3, 4]]
shallow = a[:] # ์์ ๋ณต์ฌ (๋ด๋ถ ๋ฆฌ์คํธ๋ ๊ณต์ )
shallow[0][0] = 999
print(a) # [[999, 2], [3, 4]] <-- ์ํฅ ๋ฐ์
a = [[1, 2], [3, 4]]
deep = copy.deepcopy(a) # ๊น์ ๋ณต์ฌ
deep[0][0] = 999
print(a) # [[1, 2], [3, 4]] <-- ์ํฅ ์์
(3) list * n์ผ๋ก 2์ฐจ์ ๋ง๋ค ๋ ์ฃผ์
# ์๋ชป๋ ์: ๊ฐ์ ๋ด๋ถ ๋ฆฌ์คํธ๋ฅผ ์ฐธ์กฐ
rows, cols = 3, 3
bad = [[0]cols]rows
bad[0][0] = 1
print(bad) # [[1,0,0],[1,0,0],[1,0,0]] <-- ๋ชจ๋ ๋ฐ๋
# ์ฌ๋ฐ๋ฅธ ์: ๊ฐ ํ์ ๋
๋ฆฝ์ ์ผ๋ก ์์ฑ
good = [[0 for in range(cols)] for in range(rows)]
good[0][0] = 1
print(good) # [[1,0,0],[0,0,0],[0,0,0]]
13) ๋ฏธ๋ ํ๋ก์ ํธ๋ก ์ฐ์ตํ๊ธฐ
(A) ์ฑ์ ์ฒ๋ฆฌ ํ๋ก๊ทธ๋จ (ํ๊ท /์ต๊ณ /์ ๋ ฌ)
scores = [88, 92, 76, 81, 95, 68]
print("ํ์ ์:", len(scores))
print("ํ๊ท :", round(sum(scores)/len(scores), 2))
print("์ต๊ณ ์ ์:", max(scores))
print("์ต์ ์ ์:", min(scores))
# ์์ 3๋ช
top3 = sorted(scores, reverse=True)[:3]
print("TOP 3:", top3)
# 70์ ๋ฏธ๋ง ๊ฒฝ๊ณ ๋ชฉ๋ก
warnings = [s for s in scores if s < 70]
print("๊ฒฝ๊ณ :", warnings)
(B) ์ผํ์นดํธ ์์คํ (์ถ๊ฐ/์ญ์ /์ดํฉ)
cart = []
prices = {"apple": 1000, "banana": 2000, "milk": 2500}
# ์ถ๊ฐ
cart.append("apple")
cart.extend(["banana", "milk", "apple"])
# ์ญ์ (์ฒซ ๋ฒ์งธ๋ง)
cart.remove("apple")
# ์ดํฉ ๊ณ์ฐ
total = sum(prices[item] for item in cart)
print("์นดํธ:", cart)
print("์ด์ก:", total)
(C) ํ ์คํธ ํด๋ฆฌ๋ (๊ณต๋ฐฑ/์๋ฌธ์/์ค๋ณต ์ ๊ฑฐ)
texts = [" Hello ", "WORLD", "hello", "Python ", "world "]
clean = [t.strip().lower() for t in texts]
# ์ค๋ณต ์ ๊ฑฐ(์์ ์ ์ง)
unique = []
for t in clean:
if t not in unique:
unique.append(t)
print("์ ์ :", clean)
print("์ ๋ํฌ:", unique) # ['hello', 'world', 'python']
14) ์ค์ค๋ก ํด๋ณผ ์ฐ์ต๋ฌธ์ (์ ๋ต ์์ด ๋์ !)
- ๋ฆฌ์คํธ์์ ์ต๋น๊ฐ(๊ฐ์ฅ ๋ง์ด ๋์จ ๊ฐ)์ ๊ตฌํ๋ ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์ธ์.
- ๋ฌธ์์ด ๋ฆฌ์คํธ์์ ๊ธธ์ด๊ฐ ์ง์์ธ ๋จ์ด๋ง ๋ชจ์ ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋์ธ์.
- 2์ฐจ์ ๋ฆฌ์คํธ์์ ๊ฐ ํ์ ํฉ์ ๊ตฌํด
[ํฉ1, ํฉ2, ...]ํํ๋ก ๋ฐํํ์ธ์. - ์ซ์ ๋ฆฌ์คํธ๋ฅผ ๋ฐ์ ์ค๋ณต์ ์ ๊ฑฐํ๊ณ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ์ธ์.
- ์ฃผ์ด์ง ๋ฆฌ์คํธ๋ฅผ k๊ฐ์ฉ ํ์ (rotate)ํ๋ ํจ์๋ฅผ ๊ตฌํํด๋ณด์ธ์. (์:
[1,2,3,4,5]๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก 2์นธ ํ์ →[4,5,1,2,3])
15) ์์ฝ ์ฒดํฌํฌ์ธํธ
- ๋ฆฌ์คํธ๋ ์์/๊ฐ๋ณ์ฑ์ด ํต์ฌ
- ์ธ๋ฑ์ฑ/์ฌ๋ผ์ด์ฑ์ผ๋ก ์ ๊ทผ
append,extend,insert,remove,pop,sort,sorted์์ฃผ ์ฌ์ฉ- ์ปดํ๋ฆฌํจ์ ์ผ๋ก ์งง๊ณ ๋ช ํํ๊ฒ
- 2์ฐจ์ ๋ฆฌ์คํธ๋ ์์ฑ ์ ์ฐธ์กฐ ๊ณต์ ์ฃผ์
- ๊น์ ๋ณต์ฌ ํ์ํ๋ฉด
copy.deepcopy

๋๊ธ
๋๊ธ ์ฐ๊ธฐ