Python list vs tuple vs set | Mutability· Performance
이 글의 핵심
Compare Python list, tuple, and set: ordering, duplicates, big-O operations, memory, and a decision flowchart for real code.
Introduction
“Isn’t list enough?” This guide compares list, tuple, and set so you can pick the right structure.
What you will learn
- Mutability and ordering
- Typical time costs
- Memory trade-offs
- A simple decision flow
Table of contents
- Quick comparison
- Mutability
- Performance
- Memory
- How to choose
- Common mistakes
- Advanced notes
- Closing thoughts
1. Quick comparison
| list | tuple | set | |
|---|---|---|---|
| Mutable | Yes | No | Yes |
| Order | Yes | Yes | Not for logic (CPython preserves insertion order) |
| Duplicates | Allowed | Allowed | Unique |
| Index | O(1) | O(1) | No indexing |
| Membership | O(n) | O(n) | O(1) avg |
| Append/add | amortized O(1) | — | O(1) avg |
| Memory | Moderate | Lower | Higher (hash table) |
2. Mutability
lst = [1, 2, 3]
lst.append(4)
tup = (1, 2, 3)
# tup[0] = 10 # TypeError
s = {1, 2, 3}
s.add(4)
# s[0] # TypeError — not subscriptable
3. Performance
Membership:
n = 100000
lst = list(range(n))
s = set(range(n))
# 99999 in lst # O(n)
# 99999 in s # O(1) average — much faster at scale
4. Memory
import sys
data = range(10000)
lst = list(data)
tup = tuple(data)
s = set(data)
# sys.getsizeof: tuple often < list << set for same elements
5. How to choose
graph TD
A[Pick a structure] --> B{Order matters?}
B -->|Yes| C{Need in-place edits?}
B -->|No| D[set]
C -->|Yes| E[list]
C -->|No| F{Need as dict key?}
F -->|Yes| G[tuple]
F -->|No| H{Need fastest membership?}
H -->|Yes| G
H -->|No| E
Examples:
- General ordered collection → list
- Fixed point/color/config → tuple
- Dedup / fast membership → set
- Dict keys → tuple (immutable) not list
6. Common mistakes
- Indexing a set
- Mutating a tuple in place (use new tuple)
- Using list as dict key
7. Advanced
- Set algebra:
|,&,-,^ namedtuplefor readable tuples
Closing thoughts
- Order + mutation → list
- Order + immutable → tuple
- Uniqueness / fast contains → set
- Measure if unsure
Match structures to operations and your invariants.
Related posts
Keywords
Python, list, tuple, set, data structures, time complexity, memory, comparison
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Compare Python list, tuple, and set: ordering, duplicates, big-O operations, memory, and a decision flowchart for real c… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- Python list vs tuple vs set 완벽 비교 | 자료구조 선택 가이드
- Python 자료형 | 리스트, 딕셔너리, 튜플, 세트 완벽 가이드
- Python 성능 최적화 실전 사례 | 데이터 처리 속도 100배 개선기
이 글에서 다루는 키워드 (관련 검색어)
Python, list, tuple, set, Data Structures, Performance, Comparison 등으로 검색하시면 이 글이 도움이 됩니다.