1 개념[ | ]
- Python list
- 파이썬 리스트
- 리스트는 Python에서 객체의 한 타입임
- 요소의 집합이며 인덱스로 접근 가능함
- []를 통해 생성
- ','로 요소간 분리가 가능함
2 예제 1[ | ]
Python
Copy
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors ) # ['red', 'blue', 'green', 'black', 'white']
Loading
Python
Copy
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[2] ) # green
print( colors[-1] ) # white
print( colors[-2] ) # black
Loading
Python
Copy
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[2:2] ) # []
print( colors[2:3] ) # ['green']
print( colors[2:4] ) # ['green', 'black']
Loading
Python
Copy
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[:3] ) # ['red', 'blue', 'green']
print( colors[3:] ) # ['black', 'white']
Loading
Python
Copy
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[::2] ) # ['red', 'green', 'white']
print( colors[1::2] ) # ['blue', 'black']
print( colors[0::3] ) # ['red', 'black']
Loading
3 예제 2[ | ]
Python
Copy
colors1 = ['red', 'blue', 'green']
colors2 = ['black', 'white']
print( colors1 + colors2 ) # ['red', 'blue', 'green', 'black', 'white']
print( colors2 * 2 ) # ['black', 'white', 'black', 'white']
print( 2 * colors2 ) # ['black', 'white', 'black', 'white']
Loading
4 메소드[ | ]

append() - 요소 추가
Python
Copy
numbers = [0, 1, 2, 3, 4, 5]
numbers.append(6)
print( numbers ) # [0, 1, 2, 3, 4, 5, 6]
Loading
sort() - 정렬
Python
Copy
lst = ['c', 'a', 'b']
lst.sort()
print( lst ) # ['a', 'b', 'c']
Loading
5 리스트 길이[ | ]

len() - 리스트 길이
Python
Copy
letters = ['a', 'b', 'c', 'd']
print( len(letters) ) # 4
Loading
6 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.