4  Data Structure

4.1 Dict

4.1.1 Create dict from sequence

How can I create dict from two list with keys from the first list and values from the secound?

Traditional

mapping = {}
for key, value in zip(key_list, value_list):
    mapping[key] = value

Better way:

4.1.1.1 Using the dict Constructor

keys = ['a', 'b', 'c']
values = [1, 2, 3]

# Create the dictionary using the dict constructor and zip
result_dict = dict(zip(keys, values))
result_dict
{'a': 1, 'b': 2, 'c': 3}

4.1.1.2 Using a Dictionary Comprehension

keys = ['a', 'b', 'c']
values = [1, 2, 3]

# Create the dictionary using a dictionary comprehension
result_dict = {key: value for key, value in zip(keys, values)}
result_dict
{'a': 1, 'b': 2, 'c': 3}

4.1.1.3 Handling Unequal Length Lists

from itertools import zip_longest

keys = ['a', 'b', 'c']
values = [1, 2]

# Create the dictionary using zip_longest to handle unequal lengths
result_dict = {key: value for key, value in zip_longest(keys, values)}
result_dict
{'a': 1, 'b': 2, 'c': None}

4.2 List

4.2.1 List of List is not a list

a = [1, 2, 3]
c = list(a)

a == c
a is c
False

Since the list function always creates a new Python list (i.e., a copy)

4.2.2 Append Two List

list1 = [1, 2, 3]
list2 = [4, 5, 6]

4.2.2.1 Using the + Operator

combined_list = list1 + list2
combined_list
[1, 2, 3, 4, 5, 6]

4.2.2.2 Using the extend() Method

list1.extend(list2)
list1
[1, 2, 3, 4, 5, 6]

4.2.2.3 Using the * Operator (Python 3.5+)

combined_list = [*list1, *list2, 7]
combined_list
[1, 2, 3, 4, 5, 6, 4, 5, 6, 7]

4.2.2.4 Using a List Comprehension

combined_list = [item for sublist in (list1, list2) for item in sublist]
combined_list
[1, 2, 3, 4, 5, 6, 4, 5, 6]

4.2.2.5 Using the itertools.chain() Function

import itertools

combined_list = list(itertools.chain(list1, list2))
combined_list
[1, 2, 3, 4, 5, 6, 4, 5, 6]

4.2.3 How to unnest list

Input: [["a"], ["b", "c"], ["d"]]

Desired output: ["a", "b", "c", "d"]

4.2.3.1 Using List Comprehension

nested_list =[["a"], ["b", "c"], ["d"]]
flattened_list = [item for sublist in nested_list for item in sublist]
flattened_list
['a', 'b', 'c', 'd']

4.2.3.2 Using itertools.chain

This is particularly useful for larger datasets:

import itertools

nested_list = [["a"], ["b", "c"], ["d"]]
flattened_list = list(itertools.chain(*nested_list))
flattened_list
['a', 'b', 'c', 'd']

4.2.3.3 Using sum() with an Empty List

may not be as efficient for very large lists:

nested_list = [["a"], ["b", "c"], ["d"]]
flattened_list = sum(nested_list, [])
flattened_list
['a', 'b', 'c', 'd']

4.3 Sets

a = {'a', 'b', 'c' }
b = {'c', 'd'}

print(a | b)

print(a & b)

print(a - b)

print(a ^ b) # Symmetric difference 
{'c', 'a', 'd', 'b'}
{'c'}
{'b', 'a'}
{'a', 'd', 'b'}

4.4 Operation

4.4.1 Slicing

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

a[2:5]     # [2, 3, 4]
a[:3]      # [0, 1, 2]
a[-3:]     # [7, 8, 9]
a[::2]     # [0, 2, 4, 6, 8 ]
a[::-2]    # [9, 7, 5, 3, 1 ]
a[0:5:2]   # [0, 2]
a[5:0:-2]  # [5, 3, 1]
a[:5:1]    # [0, 1, 2, 3, 4]
a[:5:-1]   # [9, 8, 7, 6]
a[5::1]    # [5, 6, 7, 8, 9]
a[5::-1]   # [5, 4, 3, 2, 1, 0]
a[5:0:-1]  # [5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]

4.4.2 Slice Naming

firstfive = slice(0, 5)
s = 'hello world'

firstfive
slice(0, 5, None)
print(s[firstfive])
hello