Python3 Data Structure
# Python3 Data Structures
## Python3.x Python3 Data Structures
In this chapter, we will mainly introduce Python data structures by combining the knowledge points learned previously.
* * *
## Lists
In Python, lists are mutable. This is the most important feature that distinguishes them from strings and tuples. In one sentence: lists can be modified, while strings and tuples cannot.
Here are the methods for lists in Python:
| Method | Description |
| --- | --- |
| list.append(x) | Adds an element to the end of the list, equivalent to a[len(a):] = . |
| list.extend(L) | Extends the list by adding all elements from the specified list, equivalent to a[len(a):] = L. |
| list.insert(i, x) | Inserts an element at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). |
| list.remove(x) | Removes the first item from the list whose value is equal to x. It raises a ValueError if there is no such item. |
| list.pop() | Removes the item at the given position in the list, and returns it. If no index is specified, a.pop() removes and returns the last item in the list. The item is then removed from the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will often see this notation in the Python Library Reference.) |
| list.clear() | Removes all items from the list, equivalent to del a[:]. |
| list.index(x) | Returns the zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item. |
| list.count(x) | Returns the number of times x appears in the list. |
| list.sort() | Sorts the items of the list in place. |
| list.reverse() | Reverses the elements of the list in place. |
| list.copy() | Returns a shallow copy of the list, equivalent to a[:]. |
The following example demonstrates most of the list methods:
## Example
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
Note: Methods like insert, remove, or sort that modify the list have no return value.
* * *
## Using Lists as Stacks
In Python, you can use a list to implement the functionality of a stack. A stack is a Last-In-First-Out (LIFO) data structure, meaning the last element added is the first one to be removed. Lists provide methods that make them well-suited for stack operations, particularly the **append()** and **pop()** methods.
You can add an element to the top of the stack using the append() method, and remove an element from the top using the pop() method without specifying an index.
### Stack Operations
* **Push**: Adds an element to the top of the stack.
* **Pop**: Removes and returns the top element of the stack.
* **Peek/Top**: Returns the top element without removing it.
* **IsEmpty**: Checks if the stack is empty.
* **Size**: Gets the number of elements in the stack.
Here is a detailed explanation of how to implement these operations in Python using a list:
### 1. Create an Empty Stack
## Example
stack = []
### 2. Push Operation
Use the append() method to add an element to the top of the stack:
## Example
stack.append(1)
stack.append(2)
stack.append(3)
print(stack) # Output: [1, 2, 3]
### 3. Pop Operation
Use the pop() method to remove and return the top element:
## Example
top_element = stack.pop()
print(top_element) # Output: 3
print(stack) # Output: [1, 2]
### 4. Peek/Top
Directly access the last element of the list (without removing it):
## Example
top_element = stack
print(top_element) # Output: 2
### 5. IsEmpty
Check if the list is empty:
## Example
is_empty = len(stack) == 0
print(is_empty) # Output: False
### 6. Size
Use the len() function to get the number of elements in the stack:
## Example
size = len(stack)
print(size) # Output: 2
### Example
Here is a complete example demonstrating how to implement a simple stack using the above operations:
## Example
class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if not self.is_empty():
return self.stack.pop()
else:
raise IndexError("pop from empty stack")
def peek(self):
if not self.is_empty():
return self.stack
else:
raise IndexError("peek from empty stack")
def is_empty(self):
return len(self.stack) == 0
def size(self):
return len(self.stack)
# Usage example
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("Top element:", stack.peek()) # Output: Top element: 3
print("Stack size:", stack.size()) # Output: Stack size: 3
print("Popped element:", stack.pop()) # Output: Popped element: 3
print("Is stack empty:", stack.is_empty()) # Output: Is stack empty: False
print("Stack size:", stack.size()) # Output: Stack size: 2
In the code above, we defined a Stack class that encapsulates a list as the underlying data structure and implements the basic operations of a stack.
The output is as follows:
Top element: 3
Stack size: 3
Popped element: 3
Is stack empty: False
Stack size: 2
* * *
## Using Lists as Queues
In Python, a list can be used as a queue, but due to the characteristics of lists, using a list directly to implement a queue is not the optimal choice.
A queue is a First-In-First-Out (FIFO) data structure, meaning the first element added is the first one to be removed.
When using a list, if you frequently insert or delete elements at the beginning of the list, performance will be affected because the time complexity of these operations is O(n). To solve this problem, Python provides `collections.deque`, which is a double-ended queue that allows efficient addition and removal of elements from both ends.
### Implementing a Queue with collections.deque
`collections.deque` is part of the Python standard library and is well-suited for implementing queues.
Here is an example of implementing a queue using deque:
## Example
from collections import deque
# Create an empty queue
queue = deque()
# Add elements to the rear of the queue
queue.append('a')
queue.append('b')
queue.append('c')
print("Queue state:", queue) # Output: Queue state: deque(['a', 'b', 'c'])
# Remove an element from the front of the queue
first_element = queue.popleft()
print("Removed element:", first_element) # Output: Removed element: a
print("Queue state:", queue) # Output: Queue state: deque(['b', 'c'])
# View the front element (without removing)
front_element = queue
print("Front element:", front_element) # Output: Front element: b
# Check if the queue is empty
is_empty = len(queue) == 0
print("Is queue empty:", is_empty) # Output: Is queue empty: False
# Get the queue size
size = len(queue)
print("Queue size:", size) # Output: Queue size: 2
### Implementing a Queue with a List
Although deque is more efficient, if you insist on using a list to implement a queue, you can do so. Here is an example of how to implement a queue using a list:
**1. Create a Queue**
## Example
queue = []
**2. Add Elements to the Rear**
Use the append() method to add elements to the rear of the queue:
## Example
queue.append('a')
queue.append('b')
queue.append('c')
print("Queue state:", queue) # Output: Queue state: ['a', 'b', 'c']
**3. Remove an Element from the Front**
Use the pop(0) method to remove an element from the front of the queue:
## Example
first_element = queue.pop(0)
print("Removed element:", first_element) # Output: Removed element: a
print("Queue state:", queue) # Output: Queue state: ['b', 'c']
**4. View the Front Element (Without Removing)**
Directly access the first element of the list:
## Example
front_element = queue
print("Front element:", front_element) # Output: Front element: b
**5. Check if the Queue is Empty**
Check if the list is empty:
## Example
is_empty = len(queue) == 0
print("Is queue empty:", is_empty) # Output: Is queue empty: False
**6. Get the Queue Size**
Use the len() function to get the size of the queue:
## Example
size = len(queue)
print("Queue size:", size) # Output: Queue size: 2
### Example (Implementing a Queue with a List)
## Example
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if not self.is_empty():
return self.queue.pop(0)
else:
raise IndexError("dequeue from empty queue")
def peek(self):
if not self.is_empty():
return self.queue
else:
raise IndexError("peek from empty queue")
def is_empty(self):
return len(self.queue) == 0
def size(self):
return len(self.queue)
# Usage example
queue = Queue()
queue.enqueue('a')
queue.enqueue('b')
queue.enqueue('c')
print("Front element:", queue.peek()) # Output: Front element: a
print("Queue size:", queue.size()) # Output: Queue size: 3
print("Removed element:", queue.dequeue()) # Output: Removed element: a
print("Is queue empty:", queue.is_empty()) # Output: Is queue empty: False
print("Queue size:", queue.size()) # Output: Queue size: 2
Although you can use a list to implement a queue, using `collections.deque` is more efficient and concise. It provides O(1) time complexity for add and remove operations, making it very suitable for a data structure like a queue.
* * *
## List Comprehensions
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. If the expression would yield a tuple, it must be parenthesized.
Here we multiply each value in the list by three:
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
Now, let's do something a bit fancier:
>>> [[x, x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
Here we call a method on each element:
## Example
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
We can use an if clause to filter:
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x >> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x*y for x in vec1 for y in vec2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]
>>> [x+y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]
>>> [vec1*vec2 for i in range(len(vec1))]
[8, 12, -54]
List comprehensions can use complex expressions and nested functions:
>>> [str(round(355/113, i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
* * *
## Nested List Comprehensions
The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.
Consider the following example of a 3x4 matrix:
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
The following list comprehension will transpose rows and columns:
>>> [[row for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
As we saw in the previous section, the inner list comprehension is evaluated in the context of the for clause that follows it, so this example is equivalent to:
>>> transposed = []
>>> for i in range(4):
... transposed.append([row for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
which, in turn, is the same as:
>>> transposed = []
>>> for i in range(4):
... # the following 3 lines implement the nested listcomp
... transposed_row = []
... for row in matrix:
... transposed_row.append(row)
... transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
* * *
## The del Statement
There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assigning an empty list to the slice). For example:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
del can also be used to delete entire variables:
>>> del a
* * *
## Tuples and Sequences
We saw that lists and strings have many common properties, such as indexing and slicing operations. They are two examples of sequence data types. Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the tuple.
A tuple consists of a number of values separated by commas, for instance:
>>> t = 12345, 54321, 'hello!'
>>> t
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
As you see, on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly; they may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).
* * *
## Sets
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
Here is a brief demonstration:
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # fast membership testing
True
>>> 'crabgrass' in basket
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
Set comprehensions are also supported:
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
* * *
## Dictionaries
Another useful data type built into Python is the dictionary. Dictionaries are sometimes found in other languages as "associative memories" or "associative arrays". Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can't use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().
It is best to think of a dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}.
Here is a small example using a dictionary:
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
The dict() constructor builds dictionaries directly from sequences of key-value pairs:
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
In addition, dict comprehensions can be used to create dictionaries from arbitrary key and value expressions:
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}
* * *
## Looping Techniques
When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
To loop over two or more sequences at the same time, the entries can be paired with the zip() function.
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
* * *
## Reference Documentation
YouTip