Data Container Conclusion
Properties and usage



Different choice of iterating method

Same function that apply on all data container
1. Get min / max value & length / number of items on data container

universal-function-for-data-container.py
py
# Define all the data container
# list
dc_list = [0, 1, 2, 3, 4]
# tuple
dc_tuple = (0, 1, 2, 3, 4)
# set
dc_set = {0, 1, 2, 3, 4}
# str
dc_str = "abcde"
# dict
dc_dict = {
"k1": 0,
"k2": 1,
"k3": 2,
"k4": 3,
"k5": 4,
}
data_container_list = [dc_list, dc_tuple, dc_set, dc_str, dc_dict]a. Length of the data container
py
# Get the length of all data container
for list_ in data_container_list:
print(len(list_))Output:
5
5
5
5
5b. Minimum value / element in data conatiner
py
# Get the min value of all data container
for list_ in data_container_list:
print(min(list_))Output:
0
0
0
a
k1c. Maximum value / element in data conatiner
py
# Get the max value of all data container
for list_ in data_container_list:
print(max(list_))Output:
4
4
4
e
k52. Data container type conversion
- using
list()to convert items to list type - using
str()to convert items to string
Example: Convert dictionary type to tuple.
py
# convert data type
# dict > tuple (only remains key)
dict_2_tuple = tuple(dc_dict)
print(dict_2_tuple)
print(type(dict_2_tuple))Output:
('k1', 'k2', 'k3', 'k4', 'k5')
<class 'tuple'>Info
All of types cannot convert (except: dict itself) to be dictionary, type as at the time converting it only received the key, without value.
3. Sorting the element of data container
py
# Sorted - Normal
for list_ in data_container_list:
print(sorted(list_))
# Sorted Reversed
for list_ in data_container_list:
print(sorted(list_, reverse=True))Output:
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
['a', 'b', 'c', 'd', 'e']
['k1', 'k2', 'k3', 'k4', 'k5']
[4, 3, 2, 1, 0]
[4, 3, 2, 1, 0]
[4, 3, 2, 1, 0]
['e', 'd', 'c', 'b', 'a']
['k5', 'k4', 'k3', 'k2', 'k1']Info
After sorting the data, all of the data will be saved as a new list, [].
Conclusion for universal fucntion that apply for all data container
