[Python] Three Methods to Reverse a List

2024. 12. 6. 12:06·Tips
Three Methods to Reverse a List

 

[::-1]

 This method reverses the list by slicing it in reverse order. It creates a new list, so if the list is very large, it can lead to unnecessary memory usage.

 

reverse()

 This method directly reverses the original list, which means it uses less memory. However, since it modifies the original list, it should not be used if you want to keep the original list intact.

 

reversed()

 This method returns an iterator, so the original list remains unchanged while using less memory. However, it cannot be used directly as a list; you need to convert it to a list using list().


Conclusion
  • If it’s acceptable to modify the original list → reverse()
  • If you need to preserve the original list and its size is small → [::-1]
  • If you need to preserve the original list and its size is very large → reversed()

 ※ While the criteria are based on the size of the list, the underlying consideration is memory usage. Larger lists consume more memory, which is why the explanation uses size as a guideline. A "small" list is defined as having around 100 elements, while a "large" list refers to those exceeding 10,000 elements.

'Tips' 카테고리의 다른 글

[IntelliJ] When Ctrl + Shift + F10 doesn't work  (2) 2024.12.09
파이썬에서 리스트를 뒤집는 세 가지 방법  (0) 2024.12.07
[Python] 파이썬의 전달 방식은 값 전달? 참조 전달?  (2) 2024.12.03
[Python] Call by Value, Call by Reference?  (2) 2024.12.02
[GitHub] 최근 커밋 완전히 삭제하기  (1) 2024.11.30
'Tips' 카테고리의 다른 글
  • [IntelliJ] When Ctrl + Shift + F10 doesn't work
  • 파이썬에서 리스트를 뒤집는 세 가지 방법
  • [Python] 파이썬의 전달 방식은 값 전달? 참조 전달?
  • [Python] Call by Value, Call by Reference?
UTACT
UTACT
시작은 가볍게 이유는 무겁게
  • UTACT
    Software Engineer
    UTACT
    • GitHub
  • 전체
    오늘
    어제
  • 공지사항

    • README
  • 최근 글

    • 분류 전체보기 (126)
      • Project Logs (2)
        • DashHub (2)
        • Re:Act (0)
        • Samsung NW (0)
      • Engineering Notes (76)
        • Java & Spring (44)
        • Database & Persistence (1)
        • DevOps & Infra (4)
        • CS & Algorithms (26)
        • Security (1)
      • Reflections (5)
        • Retrospectives (1)
        • Feedback Received (3)
        • Challenges (1)
      • Tips (24)
      • Archive (19)
  • 태그

    Python
    BOJ
    heap
    Data Type
    BFS
    도커
    vite
    iamdefinitelyabackenddeveloper
    hate-cnu
    where-was-i
    DS
    IntelliJ
    인접 노드 리스트
    .prettierrc
    듀얼 피봇 퀵 정렬
    Array
    @CreatedDate
    토스페이먼츠
    cherry-pick
    버프 슈트
    OOP
    CS
    Reallocation
    VS Code
    JPA
    REST
    타입 소거
    팀 정렬
    heapify
    데이터 영속성
  • hELLO· Designed By정상우.v4.10.6
UTACT
[Python] Three Methods to Reverse a List
상단으로

티스토리툴바