Data Types – collections — Container datatypes

Collection module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

ChainMap

groups multiple dicts or other mappings together to create a single, updateable view.
The underlying mappings are stored in a public list and can be accessed or updated by using the maps attribute. Lookups search the underlying mappings successively until a key is found. Writes, updates, and deletions only operate on the first mapping.

#ChainMap

Cosm = {
    "brand": "Maybelline",
    "toxic": False,
    "cruelty-free": True,
    "exp_date": 2024,
    "model": 123458900,
    "name": 'mascara',
    "colors": ["black", "green", "blue"]
}

base_cosm = {
    "brand": "Maybelline",
    "toxic": False,
    "cruelty-free": True,
    "exp_year": 2024
}

add_info = {
    "brand": "Maybelline",
    "name": 'mascara',
    "colors": ["black", "green", "blue"]
}

from collections import ChainMap as CM

A = list(CM(base_cosm,add_info))
print(A)
# ['brand', 'name', 'colors',
# 'toxic', 'cruelty-free', 'exp_year']
#Shows the sum of keys from all dictionaries
#Removes duplicate keys
#If a key is in more than one dictionary, 
#it takes the value from the first one

C = base_cosm.copy()
C.update(add_info)
B = list(C)
print(B)
#['brand', 'toxic', 'cruelty-free',
# 'exp_year', 'name', 'colors']
#the difference is that when copying, it adds subsequent 
#elements after the last element of the first variable

Counter

is a dict subclass for counting hashable objects. A collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts.

# Counter

from collections import Counter

Ln = [1,2,3,1,7,5,6,5,3,4,1,1,2,4]
Lw = ["love","magic","stars",
     "heart","love","magic",
     "love","love","love",
     "stars"]

c = Counter(Ln)
print(c)
#Counter({1: 4, 2: 2, 3: 2, 5: 2, 4: 2, 7: 1, 6: 1})
print(c.most_common(3))
#[(1, 4), (2, 2), (3, 2)]
print(sorted(c.elements()))
#[1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7]
print(c.total())
#14

c = Counter(Lw)
print(c)
#Counter(
# {'love': 5, 'magic': 2, 'stars': 2, 'heart': 1})
print(c.most_common())
#[('love', 5), ('magic', 2), ('stars', 2), ('heart', 1)]
c = dict((i, Lw.count(i)) for i in Lw)
print(c)
#{'love': 5, 'magic': 2, 'stars': 2, 'heart': 1}


duplicates = {}
for i in set(Lw):
    duplicates[i] = Lw.count(i)
print(duplicates)
#{'heart': 1, 'stars': 2, 'love': 5, 'magic': 2}

duplicates = {}
for i in set(Lw):
    duplicates[i] = Lw.count(i)
    print(duplicates)
    '''{'stars': 2}
    {'stars': 2, 'heart': 1}
    {'stars': 2, 'heart': 1, 'magic': 2}
    {'stars': 2, 'heart': 1, 'magic': 2, 'love': 5}'''


c = Counter(a=4, b=2, c=0, d=-2)
d = Counter(a=1, b=2, c=3, d=4)
c.subtract(d)
print(c)

Deque

generalization of stacks and queues, returns a new deque object initialized left-to-right with data from iterable. Support iteration, pickling, len(d), reversed(d), copy.copy(d), copy.deepcopy(d), membership testing with the in operator, and subscript references such as d[0] to access the first element.

# deque

W1 = "Agnieszka"
T1 = (1,2,3,5,4,7,6,9)
L1 = [1,4,3,2,0,6,5,7,8,9]

from collections import deque

d = deque(W1)

print(d)
#deque(['A', 'g', 'n', 'i', 'e', 's', 'z', 'k', 'a'])

W_part = W1[0:2] + W1[-1:]
print(W_part) #output: Aga
d = deque(W_part) #output:deque(['A', 'g', 'a'])

for elem in d:
    print(elem.upper())

W = "eve"
d = deque(W)
print(d)
#deque(['e', 'v', 'e'])

d.append("r")
d.appendleft("For")
print(d)
#deque(['For', 'e', 'v', 'e', 'r'])

d.extend("Yours")
print(d)

d.extendleft("maI")
print(d)
#deque(['I', 'a', 'm',
# 'For', 'e', 'v', 'e', 'r',
# 'Y', 'o', 'u', 'r', 's'])

# d.rotate(-1)
# print(d)
# #deque(['e', 'v', 'e', 'r', 'For'])
#
# d.pop()
# d.popleft()
# print(d)
# #deque(['e', 'v', 'e'])

W = d.copy()
print(W)

W.pop()
W.pop()
print(W)
W.popleft()
W.popleft()
W.popleft()
print(W)
#deque(['For', 'e', 'v', 'e', 'r', 'Y', 'o', 'u'])

L = deque(T1)
print(L)
#deque([1, 2, 3, 5, 4, 7, 6, 9])

L.insert(0,0)
print(L)
#deque([0, 1, 2, 3, 5, 4, 7, 6, 9])

L.remove(4) #finding a nunmber and remove it
print(L)

W.remove("Y")
W.remove("o")
W.remove("u")
print(W)
# deque(['For', 'e', 'v', 'e', 'r'])

W.reverse()
print(W)
#deque(['r', 'e', 'v', 'e', 'For'])

#other

from collections import namedtuple

Person = namedtuple("Person", ["name", "age", "city"])

p = Person("Agnieszka", 35, "Olsztyn")

print(p.name)
print(p.age)