코딩딩딩
(python) enumerate 개념 및 사용법 본문
1. enumerate 개념
enumerate는 Python의 내장 함수로 list, string, tuple 등을 반복하면서 각 항목의 인덱스와 요소를
동시에 접근 가능하게 한다.
2. enumerate 사용법 및 예제
아래 코드처럼 enmuerate를 사용할 경우, 인덱스와 그에 해당하는 요소값을 동시에 얻을 수 있다.
- 예시 코드 -
sports_brands = ['nike', 'adidas', 'newBalance']
for index, brand in enumerate(sports_brands):
print(index, brand)
- 결과 -
0 nike
1 adidas
2 newBalance
3. 번외
인덱스의 시작값을 바꿔줄 수 있다.
- 예시 코드 -
sports_brands = ['nike', 'adidas', 'newBalance']
for index, brand in enumerate(sports_brands, start= 1):
print(index, brand)
- 결과 -
1 nike
2 adidas
3 newBalance
'파이썬 > 문법' 카테고리의 다른 글
(python) 딕셔너리(dictionary) 사용법 및 예제 (0) | 2023.02.20 |
---|---|
(python) 백준 시간초과 해결 - 입출력 개선 (0) | 2023.02.18 |
(python) 한 줄에 여러 수 입력 받는 법 (0) | 2023.02.08 |
(python) 파이썬 리스트 활용 함수 (2) | 2023.01.15 |
(python) 순열, 조합, 중복순열, 중복조합 구현(Feat. itertools) (0) | 2023.01.10 |
Comments