Elements in a stack are added/removed from the top ("last in, first out") or LIFO order
stack.append(item) - добавяне на елемент
stack.pop(item) - премахване на елементElements in a queue are added/removed from the top ("first in, first out") or FIFO order
my_queue = collections.deque() - създаване на deque
създаване на deque чрез import
from collections import deque
my_queue = deque()
stack.append(item) - добавяне на елемент
stack.popleft(item) - премахване на елементTuples are a read-only collections. But the objects, inside the tuples, are mutable
a = (1, 2, 3) # tuple
b = 1, 2, 3 # tuple
c = (1, ) # tuple
d = (1) # int
a.count() - returns the number of times a specified value occurs
numbers = (1, 2, 1, 3, 1)
numbers.count(1) # 3
a.index() - returns the index of a particular element
names = ("Peter", "George", "George")
names.index("George") # 1Tuple Unpacking Allows to extract tuple elements and assign them to elements
data = (1, 2, 3)
x, y, z = data
print(x) # 1
print(y) # 2
print(z) # 3Sets are an unordered collections of items. Every element of a set is unique. Sets are mutable, so we can add or remove elements from it Sets can be used to perform mathematical set operations (union, intersection, symmetric difference, etc.)
s = {a, b, c} # type(s) -> set
a = set([1, 2, 3, 4])
b = set([3, 4, 5, 6])
a | b # a.union(b) -> {1, 2, 3, 4, 5, 6}
a & b # a.intersection(b) -> {3, 4}
a <= b # a.issubset(b) -> False
a >= b # a.issuperset(b) -> False
a - b # a.difference(b) -> {1, 2}
a ^ b # a.symmetric_difference(b) -> {1, 2, 5, 6}Multidimensional lists in Python are lists that contain other lists as their elements, creating a nested structure that can represent grids, matrices, or higher-dimensional data.
2D list (list of lists): GRID:
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Access row 1, column 2
print(matrix[1][2]) # 6
# Edit the matrix
matrix[1][2] = 10 # 103D list: CUBE
cube = [
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
]
print(cube[1][0][1]) # 6Creating them dynamically:
# each row is an independent list (list comprehension)
rows, cols = 3, 4
grid = [[0 for _ in range(cols)] for _ in range(rows)]Creating MD List with Zeros - Using loops
matrix = []
for i in range(3):
matrix.append([])
for j in range(2):
matrix[i].append(0)
# [[0, 0], [0, 0], [0, 0]]Creating 3X3 Grid with Numbers
matrix = []
for i in range(3):
matrix.append([])
for j in range(1, 4):
matrix[i].append(j)
# [[1, 2, 3], [1, 2, 3], [1, 2, 3]]Creating a matrix with zeros (using Comprehensions)
matrix = [[0 for j in range(2)] for i in range(3)]Creating a matrix with numbers (using Comprehensions)
matrix = [[j for j in range(1, 4)] for i in range(3)]Flattening a matrix (using Comprehensions)
matrix = [[1, 2, 3], [4, 5, 6]]
flattened = [num for sublist in matrix for num in sublist]
# [1, 2, 3, 4, 5, 6]