JadeCode

[Python] 개념정리 - for문 index, value 접근 본문

개발/알고리즘

[Python] 개념정리 - for문 index, value 접근

z-zero 2022. 2. 17. 11:00

1. range 이용

array = [1, 2, 5, 6, 7, 10]
for index in range(len(array)):
    print(index,array[index])
    
# 결과:
# 0 1
# 1 2
# 2 5
# 3 6
# 4 7
# 5 10

2. enumerate() 이용

array = [1, 2, 5, 6, 7, 10]
for index, value in enumerate(array):
    print(index, value)
    
# 결과:
# 0 1
# 1 2
# 2 5
# 3 6
# 4 7
# 5 10

'개발 > 알고리즘' 카테고리의 다른 글

[백준] python 8958 OX퀴즈  (0) 2022.02.25
[백준] python 2739 구구단  (0) 2022.02.25
[백준] python 1018 체스판 다시 칠하기  (0) 2022.01.24
[백준] python 1010 다리 놓기  (0) 2022.01.21
백준 1010 javascript (node js)  (0) 2022.01.14
Comments