-
[Python] enumerate() 함수Language/자료구조 2024. 5. 22. 17:11
enumerate() 함수
: 순서가 있는 자료형(list, set, tuple, dictionary, string)을 입력으로 받았을 때, 인덱스와 값을 포함하여 리턴. 인덱스와 값을 동시에 접근하면서 루프를 돌리고 싶을 때 사용
students = ['홍길동', '박찬호', '이용규', '박승철', '김지은'] for i in range(len(students)): print('{} : {}'.format(i,students[i])) print('-' * 5) for idx, value in enumerate(students): print('{} : {}'.format(idx,value))
순서가 있는 자료형 뿐만 아니라 문자열에도 적용할 수 있다.
str = 'Hello' for idx, value in enumerate(str): print('{} : {}'.format(idx,value)) >>> 0 : H 1 : e 2 : l 3 : l 4 : o
'Language > 자료구조' 카테고리의 다른 글
리스트(List) > 그 외 기능 (0) 2024.05.23 리스트(List) > 슬라이싱 (0) 2024.05.23 [Python] 리스트(List) > 리스트와 반복문 (0) 2024.05.21 [Python] 리스트(List) (0) 2024.05.21 자료구조 (0) 2024.05.20