P03-List and Dictionary#
Link to useful data
List#
In Python, a List is an ordered collection. A List can hold multiple types of elements, including numbers, strings, other data structures, and even functions. Each element is assigned an index starting from 0.
Creating Lists#
Creating and Defining a List: A List is defined using square brackets ([]) and can be assigned to a variable using the equals sign (=). Elements inside the brackets are separated by commas (,).
Example: my_list = [1, 2, 3]
Consistency of Data Types: Although a List can contain elements of different types, in practice it is often preferable to keep all elements in the List of the same type. This makes the code simpler and improves performance.
Properties of a List: Ordered, Indexable, Mutable.
Ordered: Elements retain a fixed order, and their positions are represented by indices starting at
0.Indexable: You can access elements by their indices.
Mutable: You can modify elements in a list. For example:
my_list[0] = 10changes the value at index 0.
empty_list = [] # An empty List
number_list = [1, 2, 3] # List with integers
mixed_list = [1, "two", 3.0] # List with mixed data types
Accessing Elements of a List#
By Index: You can access elements directly by their index. For example,
my_list[0]returns the first element.Slicing: You can retrieve a subset of the list using slicing. For example,
my_list[1:3]returns elements at index 1 and 2.Negative Indexing: You can use negative indices to access elements from the end of the list. For example,
my_list[-1]returns the last element.
alist = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# print whole list
print(alist)
# print the last one element
print(alist[-1])
# print the first and second elements
print(alist[0], alist[1])
# print the length of the list by function len()
print(len(alist))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
1
10 9
10
List operations#
Access elements by index:
my_list[0](returns the first element)Add elements:
append(),extend(),insert()Remove elements:
remove(),pop(),del
my_list = [1, 2, 3]
print(my_list)
my_list.append(4) # [1, 2, 3, 4]
print(my_list)
my_list.extend([5, 6]) # [1, 2, 3, 4, 5, 6]
print(my_list)
my_list.pop() # 返回 6,my_list 變成 [1, 2, 3, 4, 5]
print(my_list)
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5]
List Slicing#
my_list[1:3]→ elements at index 1 up to (but not including) 3 → indices 1 and 2my_list[:2]→ elements from index 0 up to 2 → indices 0 and 1my_list[2:]→ elements from index 2 to the endmy_list[-1]→ the last elementmy_list[-3:-1]→ elements from the third-to-last up to (but not including) the last
# print the 1 to 2 elements
alist = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
print(alist[0:2])
print(alist[:2])
# print the last 3 to 2 elements from the end
print(alist[-3:-1])
print(alist[:3])
print(alist[:-3])
print(alist[3:])
print(alist[-3:])
[10, 9]
[10, 9]
[3, 2]
[10, 9, 8]
[10, 9, 8, 7, 6, 5, 4]
[7, 6, 5, 4, 3, 2, 1]
[3, 2, 1]
Updating Elements in a List#
Updating elements in a Python list can be done in several ways, and three common methods are append(), extend(), and the + operator. While they may look similar in name or usage, they serve different purposes and produce different results.
append() The append() method adds a single element to the end of a list. If the element you append is itself another list, then that entire list will be added as a single element at the end of the original list.
extend() The extend() method is used to merge two lists. Instead of adding the second list as a single element, it appends each element of the second list to the end of the first list.
alist = [1, 2, 3]
blist = [3, 2, 1]
alist.append(2) # 結果會是 [1, 2, 3, 2]
print(alist) #
alist.append(blist)
print(alist) # 結果會是 [1, 2, 3, 2, [3, 2, 1]]
[1, 2, 3, 2]
[1, 2, 3, 2, [3, 2, 1]]
alist = [1, 2, 3]
blist = [3, 2, 1]
alist.extend(blist) # 結果會是 [1, 2, 3, 3, 2, 1]
print(alist)
alist.append(blist)
print(alist)
[1, 2, 3, 3, 2, 1]
[1, 2, 3, 3, 2, 1, [3, 2, 1]]
+ operator#
The + operator can also be used to combine lists. Unlike append() or extend(), using + does not directly modify the original list. Instead, it creates a new list, and you need to reassign it to the original variable if you want the change to take effect.
alist.extend(blist)modifies the originalalistso that it now includes all the elements ofblist.alist = alist + blistoralist += blistachieves the same result of merging two lists, but it requires reassignment toalist.
alist = [1, 2, 3]
blist = [3, 2, 1]
print(alist + blist)
print(alist)
alist.extend(blist)
print(alist)
[1, 2, 3, 3, 2, 1]
[1, 2, 3]
[1, 2, 3, 3, 2, 1]
Access Nested List#
Nested list. A list in Python can contain not only basic data types such as integers, floats, and strings, but also other data structures, including another list. When a list contains another list, we call it a nested list. For example:
nested_list = [[1, 2], [3, 4]]
Accessing elements in a nested list is slightly more complex because each element may itself be a list. For example, with nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], using nested_list[0] will return [1, 2, 3]. If you want to access a specific element inside the sublist, such as the second element in the first sublist (which is 2), you can use nested_list[0][1].
Multi-level indexing. In nested lists, you can use multi-level indexing. In the example nested_list[0][1], the first index 0 refers to the position in the outer list, while the second index 1 refers to the position in the inner list.
Updating elements in a nested list works similarly to updating a regular list, except you need to specify multiple indices. For example, to change the number 2 to 20 in the example above, you would write: nested_list[0][1] = 20.
You can also traverse a nested list using nested loops. For example:
for sublist in nested_list:
for element in sublist:
print(element)
mlist = [[1, 2, 3, 4, 5, 6, 7],
[11, 12, 13, 14, 15, 16, 17],
[21, 22, 23, 24, 25, 26, 27]]
print(mlist[2][5])
26
alist = [1, 2, 3, 4, 'abc', '123', '123.1', [1, 2, 3] ]
print(len(alist)) #length len()
print(alist[7], type(alist[7]))
# Access any elements in the second layers of alsit
print(alist[-1][2]) # Review: -1 indicates the last element in a list
8
[1, 2, 3] <class 'list'>
3
Dictionary#
In Python, a Dictionary (often called dict) is a very flexible and widely used data structure. A dictionary stores data in key–value pairs. Unlike a list, the elements of a dictionary are not stored in a specific order. This means you cannot access them by an index; instead, you use their corresponding keys.
Creating and Defining a Dictionary: A dictionary is defined using curly braces
{}. Example:my_dict = {'key1': 'value1', 'key2': 'value2'}Properties of a Dictionary:
Unordered
Stores data in key–value pairs
Mutable (can be modified)
Rules for Keys and Values:
Keys must be of an immutable type: integers, strings, floats, or tuples.
Lists or other dictionaries cannot be used as keys because they are mutable and may cause inconsistent behavior.
Values can be of any type, including lists, tuples, or even other dictionaries.
Nested Dictionaries: A dictionary can contain another dictionary, forming a nested structure. Example:
nested_dict = {'outer': {'inner': 1}}Dictionary Functions and Methods:
.keys()→ returns all keys.values()→ returns all values.items()→ returns all key–value pairs.get(key, default)→ returns the value for a given key, avoidsKeyErrorif the key is missing.update()→ updates or merges dictionaries
Alternatives from the
collectionsmodule:defaultdict()→ automatically creates a key with a default value if it does not existCounter()→ counts the frequency of elements
Practice Example: YouBike Data Processing
Read data from a URL
Convert it to JSON format
Extract the required information from the JSON
Convert the result into a List or Dictionary for analysis
adict = {1:3, '2':4}
print(adict[1], adict['2'])
alist = [1, 2, 3, "a", 'b', 'c', [1, 2, 3], [4, 5, 6]]
print(len(alist))
adict = {'3':3, 2:4, '3':5, '4':6, 1.13: '123', 'a': alist}
print(len(adict))
print(type(adict))
print(adict['a'][6][1])
bdict = {1:alist, 2:adict}
print(bdict[2]['a'][6][1])
3 4
8
5
<class 'dict'>
2
2
Accessing dictionary#
在Python中,存取dict中的值主要是通過其對應的鍵(key)來實現。以下是一些常見的操作方式:
印出整個字典:使用
print()函數可以輸出整個字典的內容。例如以下例子會會列印出bdict字典中所有的鍵值對。print(bdict)
使用鍵來存取值:如果你知道某個鍵(key),就可以直接用這個鍵來獲取其對應的值(value)。下例這將會打印出與鍵
'3'相關聯的值。print(adict['3'])
獲取所有鍵和值:你可以使用
.keys()和.values()方法來分別獲取所有的鍵和值。下例.keys()將返回一個包含所有鍵的列表,而.values()將返回一個包含所有值的列表。print(adict.keys()) print(adict.values())
# access the dictionary
# print whole dict
print(bdict)
# print the value of key '1'
print(adict['3'])
# print the value of key 1
print(adict.keys())
print(adict.values())
# print the first element of the list mapped by key 4
{1: [1, 2, 3, 'a', 'b', 'c', [1, 2, 3], [4, 5, 6]], 2: {'3': 5, 2: 4, '4': 6, 1.13: '123', 'a': [1, 2, 3, 'a', 'b', 'c', [1, 2, 3], [4, 5, 6]]}}
5
dict_keys(['3', 2, '4', 1.13, 'a'])
dict_values([5, 4, 6, '123', [1, 2, 3, 'a', 'b', 'c', [1, 2, 3], [4, 5, 6]]])
Counter() and defaultdict()#
There are two data structures that are very similar to a dictionary: defaultdict() and Counter().
defaultdict()#
defaultdict() is a very convenient data structure from the collections module. Its main advantage is that when you try to access a key that does not exist, it automatically initializes that key instead of raising a KeyError.
When creating a defaultdict, you can pass in a factory function that generates default values. For example:
from collections import defaultdict
# Create a defaultdict with int as the factory function
# Each new key will automatically start with value 0
counts = defaultdict(int)
counts['apple'] += 1
counts['banana'] += 1
counts['apple'] += 1
print(counts)
# Output: defaultdict(<class 'int'>, {'apple': 2, 'banana': 1})
from collections import defaultdict
adict = defaultdict(list)
adict['a'].append(1)
adict['b'].append(2)
adict['a'].append(3)
print(adict)
bdict = defaultdict(int)
bdict['a'] += 1
bdict['b'] += 1
print(bdict)
defaultdict(<class 'list'>, {'a': [1, 3], 'b': [2]})
defaultdict(<class 'int'>, {'a': 1, 'b': 1})
defaultdict()與 dict 的比較::使用普通的 dict,你需要手動檢查鍵是否存在或使用 setdefault 方法:
d = {}
d.setdefault('a', []).append(1)
Counter()#
Counter是另一個來自於collections模塊的資料結構,專門用於計數。它非常適合於頻率計數等應用。Counter可以使用most_common(n)方法來獲取出現次數最多的n個元素。Counter與dict的比較::
使用普通的dict進行計數會相對複雜,需要檢查每個鍵是否已經存在於字典中:
from collections import Counter
c = Counter(['a', 'b', 'c', 'a', 'b', 'a'])
print(c)
print(c.most_common(2))
Counter({'a': 3, 'b': 2, 'c': 1})
[('a', 3), ('b', 2)]