[2026] Python list vs tuple vs set | Mutability, Performance, and How to Choose

[2026] Python list vs tuple vs set | Mutability, Performance, and How to Choose

이 글의 핵심

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

  1. Quick comparison
  2. Mutability
  3. Performance
  4. Memory
  5. How to choose
  6. Common mistakes
  7. Advanced notes
  8. Closing thoughts

1. Quick comparison

listtupleset
MutableYesNoYes
OrderYesYesNot for logic (CPython preserves insertion order)
DuplicatesAllowedAllowedUnique
IndexO(1)O(1)No indexing
MembershipO(n)O(n)O(1) avg
Append/addamortized O(1)O(1) avg
MemoryModerateLowerHigher (hash table)

2. Mutability

아래 코드는 python를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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: 아래 코드는 python를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

아래 코드는 python를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

아래 코드는 mermaid를 사용한 구현 예제입니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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: |, &, -, ^
  • namedtuple for readable tuples

Closing thoughts

  1. Order + mutation → list
  2. Order + immutable → tuple
  3. Uniqueness / fast contains → set
  4. Measure if unsure
    Match structures to operations and your invariants.


Keywords

Python, list, tuple, set, data structures, time complexity, memory, comparison

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3