Data Structures in Python
Python provides a bunch of data structures in distinct forms for distinct goals. In the high level, we can group these by below categories:
- Sequences Types: List, Tuple, Range, String (we've talked about this)
- Set Types: Set, Frozen Set
- Mapping Types: Dictionary
All those common types apply some common operations in the following table.
Based on the ability to re-define items, sequences are categorized into these groups:
- Immutable sequence
- Mutable sequence. Common operations applied for mutable sequences are mentioned in the following table.
Lists
- A mutable sequence
- Used to store collections of homogeneous items (or heterogenous items)
- Can be constructed in several ways:
- square brackets for empty list:
[]
- square brakets and separating items with commas:
[a]
,[a, b, c]
- using list comprehension:
[x for x in iterable_list]
(later) - using a constructor fucntion:
list()
,list("abc")
,list((1, 2, 3))
- square brackets for empty list:
Let's try to create some lists!
Let's applies some operations in the previus tables.
Tuples
- An immutable sequence
- Used to store collections of heterogeneous data (or homogeneous) that is rarely changed
- Can be constructed in several ways below:
- pair of parentheses to denote emtpy tuple:
()
- trailing comma for a singleton tuple:
a,
, `(a,) - separating items with comma:
a, b, c
, or(a, b, c)
- use constructor function
tuple()
- pair of parentheses to denote emtpy tuple:
Remember that since tuple is immutable, we can't modify the items after assignment.
Let's create some tuples!
Set Types
- An unordered collection
- Unordered collection means it can't be indexed and sliced (or other sequence-like behavior)
set
is mutable (the content can be changed), butfrozenset
is immutable.- Usually used to remove duplicates, math operations in set (e.g. intersection, etc.)
- Can be constructed in several ways below:
- use comma-separated list of items within curly braces:
{'hack', 'active'}
- use a set comprehension (similar to list comprehension) <- later on this
- use constructor function
set()
- use comma-separated list of items within curly braces:
Unordered collection means that it can be indexed since Python doesn't know which item is the first to be indexed.
Hence, doing all_set[0]
will throw an error. Let's catch one!
Dictionary
- A mutable collection
- A mapping object which consists of key-value pairs
- A key in dictionary is almost arbitrary values (except unhashable object, e.g. lists, dictionary, or other mutable objects)
- Can be constructed in several ways:
- using comma-separated list of
key: value
pairs within curly braces:{"jack": 2048, "paul": 4127}
- using constructor function
dict()
- using dictionary comprehension
- using comma-separated list of
Case Study
1. Finding Nemo
Given a list of fish, using looping, we will find if a fish has a string nemo
in it.
list_fishes = ["emo", "sprout", "shark", "nemoo", "bynemo", "neemoo", "turtleish", "moo", "nemmo", "nemo"]
2. Merging Data into Dictionary
Given 2 list of data, names
and credits
, using looping, create a dictionary transactions
that consists of keys names
and the value for data names
and credits
consecutively.
names = ["michael", "lucifer", "dosan", "queen"]
credits = [109.29, 549.919, 800.13, 949.01]