In this tutorial, you'll learn how to use an OrderedDict (Ordered Dictionary) In Python.
—
Facebook: / gokcedbsql
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn 10 ways to use the ordered dictionary from the collections module in Python. Number one, order dictionaries or dictionaries that remember insertion order.
Here I'm starting by creating an empty order dict object followed by storing the table names as keys and the number of columns as values. As you can see in the output insertion order was maintained. Number two, you can also pass it table of items as an argument to the constructor to create a brand new order dict object.
Number three, another way to create an ordered dictionary is by passing keyword arguments to the constructor. Number four, you can also use them from the keys method to create an ordered dictionary with a default value. Here I'm setting a default value of 0 for all my keys then later updating the values one by one.
Number five, use the del keyword to delete an item from an ordered dictionary. Number six to iterate over the items of an ordered dictionary, you can use a for loop with the items method. Number seven, make use of the reversed function to iterate over the items in reverse order.
Number eight, leverage the move-to-end method to move an item to the end of the ordered dictionary. Number nine, use the pop item method to remove an item from the very end of an ordered dictionary. Last but not the least number ten.
Make use of the pipe and the pipe equal to operators to merge and update order dictionaries. In this example, I'm first merging the 10 a and 10 b order dictionaries to 10c and then later updating 10b with 10a. There you have it.
Make sure you like, subscribe, and turn on the notification bell. Until next time.
Connect MySQL In Python: • How To Connect To MySQL In Python (2 ...
Run Selenium PyTests In Parallel: • How To Run Selenium PyTests In Parall...
Find_elements() in Selenium: • How To: Find_Elements() In Selenium (...
—
from collections import OrderedDict
1. OrderedDicts are dictionaries that remembers insertion order
start by creating an empty OrderedDict object
table_od1 = OrderedDict()
table_od1["property_info"] = 5
table_od1["customer_info"] = 4
table_od1["sales"] = 6
print("1.", table_od1)
2. can also pass iterable of items as an argument to the constructor
table_od2 = OrderedDict([("property_info", 5), ("customer_info", 4), ("sales", 6)])
print("2.", table_od2)
3. keyword arguments to the constructor
table_od3 = OrderedDict(property_info=5, customer_info=4, sales=6)
print("3.", table_od3)
4. fromKeys() method and then update values
keys4 = ["property_info", "customer_info", "sales"]
table_od4 = OrderedDict.fromkeys(keys4, 0)
print("4a.", table_od4)
table_od4["property_info"] = 5
table_od4["customer_info"] = 4
table_od4["sales"] = 6
print("4b.", table_od4)
5. del to delete an item
table_od5 = OrderedDict(property_info=5, customer_info=4, sales=6)
del table_od5["property_info"]
print("5.", table_od5)
6. iterate over items using for loop and items() method
table_od6 = OrderedDict(property_info=5, customer_info=4, sales=6)
for key, value in table_od6.items():
print("6.", key, value)
7. use reversed function to iterate in reverse order
table_od7 = OrderedDict(property_info=5, customer_info=4, sales=6)
for key, value in reversed(table_od6.items()):
print("7.", key, value)
8. move_to_end()
table_od8 = OrderedDict(property_info=5, customer_info=4, sales=6)
table_od8.move_to_end("property_info")
print("8.", table_od8)
9. popitem()
table_od9 = OrderedDict(property_info=5, customer_info=4, sales=6)
print("9a.", table_od9)
table_od9.popitem()
print("9b.", table_od9)
10. Merging and updating OrderedDicts using operators
table_od10a = OrderedDict(property_info=5, customer_info=4)
table_od10b = OrderedDict(sales=6)
table_od10c = table_od10a | table_od10b # merge
print("10c.", table_od10c)
table_od10b |= table_od10a # update
print("10b.", table_od10b)
Информация по комментариям в разработке