다른 명령
2번째 줄: | 2번째 줄: | ||
{| class="wikitable" | {| class="wikitable" | ||
|+ List, Dictionary, Tuple 비교 | |+ List, Dictionary, Tuple 비교 | ||
! | ! 구분 !! List !! Dictionary !! Tuple | ||
|- | |- | ||
! **정의** | ! **정의** | ||
| Ordered, mutable sequence of items|Unordered, mutable collection of key-value pairs | Ordered, immutable sequence of items | | Ordered, mutable sequence of items | ||
|Unordered, mutable collection of key-value pairs | |||
| Ordered, immutable sequence of items | |||
|- | |- | ||
! **사용 문법** | ! **사용 문법** | ||
| `[1, 2, 3]` | `{'key1': 'value1', 'key2': 'value2'}` | `(1, 2, 3)` | | `[1, 2, 3]` | ||
| `{'key1': 'value1', 'key2': 'value2'}` | |||
| `(1, 2, 3)` | |||
|- | |- | ||
! **Mutable?** | ! **Mutable?** | ||
20번째 줄: | 24번째 줄: | ||
|- | |- | ||
! ** 사용예시 ** | ! ** 사용예시 ** | ||
| Storing collections of items where order matters | Storing key-value pairs for fast lookup | Storing fixed collections that should not change | | Storing collections of items where order matters | ||
| Storing key-value pairs for fast lookup | |||
| Storing fixed collections that should not change | |||
|- | |- | ||
! **언제 사용 하나?** | ! **언제 사용 하나?** | ||
| When you need an ordered and mutable sequence | When you need to associate values with unique keys | When you need an ordered and immutable collection | | When you need an ordered and mutable sequence | ||
| When you need to associate values with unique keys | |||
| When you need an ordered and immutable collection | |||
|- | |- | ||
! **Example Code** | ! **Example Code** | ||
| < | | <source lang="python"> | ||
my_list = [1, 2, 3] | my_list = [1, 2, 3] | ||
print(my_list[0]) # Output: 1 | print(my_list[0]) # Output: 1 | ||
</ | </source> | ||
| < | |<source lang=python> | ||
my_dict = {'name': 'Alice', 'age': 25} | my_dict = {'name': 'Alice', 'age': 25} | ||
print(my_dict['name']) # Output: Alice | print(my_dict['name']) # Output: Alice | ||
</ | </source> | ||
| < | |<source lang=python> | ||
my_tuple = (1, 2, 3) | my_tuple = (1, 2, 3) | ||
print(my_tuple[1]) # Output: 2 | print(my_tuple[1]) # Output: 2 | ||
</ | </source> | ||
|} | |} |
2024년 10월 21일 (월) 20:42 판
튜플 딕셔너리 리스트 차이점 ,특징
구분 | List | Dictionary | Tuple |
---|---|---|---|
**정의** | Ordered, mutable sequence of items | Unordered, mutable collection of key-value pairs | Ordered, immutable sequence of items |
**사용 문법** | `[1, 2, 3]` | `{'key1': 'value1', 'key2': 'value2'}` | `(1, 2, 3)` |
**Mutable?** | Yes | No | ||
**Indexed Access** | Yes (by key) | Yes (by index) | ||
**증복허용 여부 ** | No (keys must be unique) | Yes | ||
** 사용예시 ** | Storing collections of items where order matters | Storing key-value pairs for fast lookup | Storing fixed collections that should not change |
**언제 사용 하나?** | When you need an ordered and mutable sequence | When you need to associate values with unique keys | When you need an ordered and immutable collection |
**Example Code** | my_list = [1, 2, 3] print(my_list[0]) # Output: 1 |
my_dict = {'name': 'Alice', 'age': 25} print(my_dict['name']) # Output: Alice |
my_tuple = (1, 2, 3) print(my_tuple[1]) # Output: 2 |