Python enumerate

Happy_JG·2023년 6월 20일

Python

목록 보기
4/12
post-thumbnail

Enumerate

시퀀스의 인덱스와 요소를 함께 뽑아낼 때 사용

dic = dict()
for key, value in dic.items():
#딕셔너리를 다뤄본 경험이 있다면 반드시 썼을 코드

딕셔너리를 굳이 쓸 필요가 없거나, 시퀀스의 인덱스와 요소를 함께 뽑아낼 때 혹은 사용할 때 요긴하게 쓸 수 있는(?) enumerate함수. (많이 사용하는 지는 잘 모르겠다.)

lst = [3,6,7,8,9]
for index,value in enumerate(lst):
    print(index+1,value)
1 3
2 6
3 7
4 8
5 9

혹은

for word in enumerate(['A','B','C'])
	print(word)
(0,'A')
(1,'B')
(2,'C')

실제 적용 코드는 추후에 작성할 것!

profile
hello!

0개의 댓글