0. ๋ฆฌ์คํธ List ๋ ?
์ด๋ ํ ์๋ฃํ๋ ํฌํจ์ํฌ ์ ์๋ ์๋ฃํ
๋น์ด์์ ์๋ ์๊ณ ์ซ์, ๋ฌธ์, ์ซ์์ ๋ฌธ์๋ฅผ ํจ๊ป ๊ฐ์ง ์๋ ์์ผ๋ฉฐ ๋ฆฌ์คํธ๋ฅผ ์์๋ก ํฌํจํ ์๋ ์๋ค
# ๋ฆฌ์คํธ๋ช
= [์์1, ์์2, ์์3, …]
# ListName = [1, 2, 3, …]
a = []
b = [1, 2, 3]
c = ["this", "is", "Python"]
d = [1, 2, "this", "Python"]
e = [[1, 2], "this", [3, "Python"]]
1. ๋ฆฌ์คํธ์ ์ธ๋ฑ์ฑ
๋ฆฌ์คํธ ์์ ๋ฆฌ์คํธ๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ค๋ฅธ ์ธ์ด์ 2์ฐจ์ ๋ฐฐ์ด๊ณผ ๊ฐ์ด ์ธ๋ฑ์ฑ ํ ์ ์๋ค
print(b) # [1, 2, 3]
print(b[0], b[1], b[2]) # 1 2 3
print(c) # ['this', 'is', 'Python']
print(c[0], c[1], c[2]) # this is Python
print(d) # [1, 2, 'this', 'Python']
print(d[0], d[1], d[2], d[3]) # 1 2 this Python
print(e) # [[1, 2], 'this', [3, 'Python']]
print(e[0], e[1], e[2]) # [1, 2] this [3, 'Python']
print(e[0][0], e[0][1]) # 1 2
print(e[2][0], e[2][1]) # 3 Python
2. ๋ฆฌ์คํธ์ ์ฌ๋ผ์ด์ฑ
[a:b] ๋ผ๋ฉด a <= i < b ์ ๋ฒ์๋ง ํฌํจ๋๋ค. ์ฆ, b[0:1]์ ๊ฒฝ์ฐ b[0]๋ง ์ถ๋ ฅ๋๋ค
print(b[0:1]) # [1]
print(b[1:3]) # [2, 3]
print(c[0:]) # ['this', 'is', 'Python']
print(c[:-1]) # ['this', 'is']
print(d[2:]) # ['this', 'Python']
print(d[:2]) # [1, 2]
print(e[0:2]) # [[1, 2], 'this']
3. ๋ฆฌ์คํธ์ ์ฐ์ฐ
+ ์ฐ์ฐ์ : ํผ์ฐ์ฐ์ ๋ฆฌ์คํธ๋ฅผ ํฉ์น๋ ๊ธฐ๋ฅ์ ํ๋ค
* ์ฐ์ฐ์ : ํผ์ฐ์ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋ฐ๋ณตํ๋ค
f = c + b
print(f) # ['this', 'is', 'Python', 1, 2, 3]
g = d * 2
print(g) # [1, 2, 'this', 'Python', 1, 2, 'this', 'Python']
4. ๋ฆฌ์คํธ์ ์์ ๊ณผ ์ญ์
์ธ๋ฑ์ค ๊ฐ์ ์๋ก ์ ์ฅํ์ฌ ์์๊ฐ ์์ ์ด ๊ฐ๋ฅํ๋ฉฐ delํจ์๋ฅผ ์ด์ฉํ์ฌ ์ญ์ ํ ์ ์๋ค
์ฃผ์ : 4์ค์ g[0]์ ๋๋ฒ ์ญ์ ํ๋๋ฐ ๊ธฐ์กด์ g[0]๊ณผ g[1]์ด ์ญ์ ๋์๋ค.
g[3] = "is"
g[4] = "Java"
print(g) # [1, 2, 'this', 'Python', 1, 2, 'this', 'Python']
del g[0], g[0]
del g[3:]
print(g) # ['this', 'is', 'Java']
del g
print(g) # ์ค๋ฅ NameError: name 'g' is not defined