리스트의 size를 계산할 때 len()
함수를 사용하면 됩니다.
len()
len()
은 다음과 같이 인자로 전달된 sequence(list, tuple 등)의 크기를 리턴합니다.
len(s)
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
list 또한 collection 및 sequence에 해당하며, 다음과 같이 len()
으로 크기를 계산할 수 있습니다.
my_list = []
print(len(my_list))
my_list = [10, 22, 19]
print(len(my_list))
Output:
3
0
tuple, dict, range
tuple, dictionary, range의 크기도 len()
으로 계산할 수 있습니다.
my_tuple = (10, 22, 19, 23)
print(len(my_tuple))
my_dict = {1: 'a', 2: 'b'}
print(len(my_dict))
my_range = range(1, 10)
print(len(my_range))
Output:
4
2
9
byte, string
byte와 string의 크기도 len()
으로 계산할 수 있습니다.
str = 'Python'
print(len(str))
byte = b'Python'
print(len(byte))
Output:
6
6
참고
Recommended Posts:
- Python - dict 정렬 (Key, Value로 sorting)
- Python - String을 bytes로 변경하는 방법
- Python - bytes를 String으로 변환하는 방법
- Python - 리스트에서 첫번째, 마지막 요소 가져오는 방법
- Python - 문자열에서 특정 단어 추출
- Python - float을 int로 변경하는 방법
- Python - 특정 문자열로 시작하는 문자열 찾기
- Python - dictionary의 중복 제거 방법
- Python - 리스트가 비어있는지 확인
- Python - 문자열 뒤집기, 문자열 거꾸로 출력
- Python - 현재 날짜, 시간 가져오는 방법
- Python - 두개의 리스트 하나로 합치기
- Python - 2차원 리스트를 1차원 리스트로 만들기