Python리스트sorted
copy

sorted리스트

list

정렬된 새 리스트를 반환합니다.

문법

sorted(iterable, key?, reverse?)

예제

아래 값을 입력하면 예제에 즉시 반영됩니다.

nums
result
sorted
print
words
key
nums = [3, 1, 4, 1, 5, 9]
result = sorted(nums)
print(result)  # [1, 1, 3, 4, 5, 9]
print(nums)    # [3, 1, 4, 1, 5, 9]  (원본 유지)

words = ["banana", "apple", "cherry"]
print(sorted(words, key=len))
# ['apple', 'banana', 'cherry']