본문 바로가기
프로그래밍 언어/Python

[python] 리스트 역순으로 반복문 실행하기

by nahkim 2023. 4. 10.

반복문에 reversed 함수를 사용

nums = [1, 2, 3, 4, 5]

for num in nums:
	print(num, end=" ")
# 1 2 3 4 5

print()

for num in reversed(nums):
	print(num, end=" ")
# 5 4 3 2 1