본문 바로가기

Python

Python_G-007. List vs Tuple vs set vs dictionary

G-007_List_Tuple_Set_Dictionary.py
0.00MB

 

Feature List Tuple Set Dictionary
Syntax using square brackets
[   ]
using parentheses
(   )
using curly braces
{   }
using curly braces
{ 'key': 'value', 'key2': 'value2' }
Example [1, 2, 3] (1, 2, 3) {1, 2, 3} {'name': 'Alice', 'age': 30}
Mutability Mutable Immutable Mutable Mutable
Ordered Yes Yes No Yes
Allow
Duplicates
Yes Yes No Keys: No,
Values: Yes
Methods Many built-in methods
 - append( item )
 - remove( item )
 - sort( )
 - pop( index )  
Few built-in methods
 - count( item )
 - index( item )
Many built-in methods Many built-in methods
Use Cases Ordered,
changeable collection
Ordered,
unchangeable collection
Unordered,
unique collection
Key-value pairs
Performance General-purpose Fast read access Fast membership testing Fast lookups with keys

ps. lists don't directly support arithmetic operations

ex) 

a = [1, 2, 3]

b = [3, 5, 7]

c = a + b

print( c)    # output : [1, 2, 3, 3, 5, 7]

 

==> that's why we need to use NumPy.