Collections in Python - Advanced Python 06 - Programming Tutorial
Key Takeaways
The video covers the collections module in Python, including the counter, named tuple, ordered dictionary, default dictionary, and deck. It demonstrates how to import and use these data structures with examples.
Full Transcript
hi everybody welcome to a new Python tutorial today we're going to talk about the collections module in Python the collections module implements special container datatypes and provides alternatives with some additional functionality compared to the general built in containers like dictionaries lists or tuples so we will be talking about five different types from the collections module the counter the named tuple the order sticks the default dict and the deck so let's start with the counter and first of all we have to import it from collections import counter and the counter is a container that stores the elements as dictionary keys in their counts as dictionary values so let's say we have a string called a with some different characters AAA BBB CCC and then we can create our counter we say my counter equals counter and then we give it our string and if we print it then we see we have a dictionary with all the different characters as keys and their count as values so we have five times a four times B and three times C and like with a normal dictionary we can have a look at only the items so this will give us all the key value pairs we can have a look at the keys so this will give us an iterable over the keys and we can also only have a look at the values so this will give us all the different values and what's also very helpful is to have a look at the most common element in our counter dictionary so we say if we first print our counter again and then we can say we want to print my counter dot most common and then here how many different items so I want to see only the very first so the the most common um element so if I print this then I will get the a with the count 5 is the most common element so if I say 2 here then it will give me the two most common types so it will also put the B in here and this will return a list with tuples in it so for example if I want to have a look at only the I want to see what is the most common element then I will except to access the index 0 so this will give us the tuple and index 0 and then if I only want to see the element then I will again have to access the first element of this tuple so again 0 and then I will get the a is the element that is most common in our string so we can also use a list here or any other iterable yeah we can also have a list with all all the different elements so if we say print my counter dot elements and this will give us an iterable over elements repeating each as many times as it counts so I have to convert this to a list in order to print it nicely so now if I print it and I will see I will get all the different elements here as a list and I can for example iterate over this so that's the counter next talk about the name stupa and of course first of all we have to import it so we say from collections import named tuba and the named tuples is an easy to create and lightweight object type similar to a struct so what I can do is I can define my named tuple I say for example let's create a 2d point and call it point equals and then I will say named tuple and then as first argument I give it the class name so typically this is the same name that I used here and then as a second argument I use another string and here I use all the different fields I want separated by either a comma or a space so I can say X comma Y so this will create a class called point with the fields x and y so now I can create this point so I can say PT equals point and then I will give it well used for x and y so for example I will give it 1 and minus 4 and now if I print my point then I will see I have a point with x equals 1 and y equals minus 4 and I can also access the field so I can say PT dot X and Pt dot y so then this will print the values for x and y next is the ordered dictionary so from collections import ordered dict and the ordered stick is just like a regular dictionary but they remember the order that the items were inserted so they have become less important now since the built in dictionary class has also the ability to remember the order since Python 3.7 this is guaranteed but for example if you use an older Python version this may be a way to use a dictionary that remembers the order so for example let's create a dictionary like so and then we can append key value pairs like with a normal dictionary so we say here and brackets give it a key a and a value 1 and let's do this with some more key values so let's say we have B C and D and 2 3 4 and now if you print this then we see it's the same order as we inserted it so for example if we inserted the a at the very end then it will also get printed at the end of our ordered dictionary yeah since here I'm using 3 Python three-point-seven so in this I can also just simply use a normal dictionary now and it still remembers the order next we have a look at the default stick so from collections import default stick and the default tick is also similar to the usual dictionary container with the only difference that it will have a default value if the key has not been set yet so what we will do we have to create a default stick and as an argument we will give it a default type so let's say we want to have an INT an integer here as default type and then we can fill our dictionary again let's say D with the key a is 1 and D with the key B equals 2 and let's print our dictionary so we will see it here and then we can access the key so for example it's XS the key a and then it will give 1 and the key B will return - and now if I put in a key that does not exist so for example C then what will happen it will return the default value of an integer and this is by default a zero so I can also for example say I want a float default value so then this will return 0.0 if it does not exist or for example I will have an empty list if it does not exist so yeah with a normal dictionary this would raise a key error so now this would raise a key error but with the default ticked it would return the default value of the type that we specify so as a last collections type we will talk about the deck so the deck is a double ended queue and it can be used to add or remove elements from both ends and both are implemented in a way that this will be very efficiently and yeah let's create a deck so let's say D equals deck and then we can append items like with a list let's say D append 1 and D append 2 and then print it now now we see our deck here and also we can say we can say D dot append left so this will add elements at the left side so now we can see our 3 got added here and we can also again remove elements from both sides so we can say d dot pop and now if we print our deck then we will see that with pop this will return and remove the last element so now the two got removed or we can say d dot pop left so this will return and remove the element from the left side so now the 3 got removed you can also of course a d dot clear so this will remove all elements we can extend our deck with multiple elements at a time so we can see d dot extend and then give it a list let's say 4 5 6 so this will add all the elements at the right side or we can say d dot extend left so this will extend all the elements at the left side and note that now it will at first the 4 from the left side and the 5 and then the 6 so now 6 is the most left element in our deck we can also rotate our text so we can say d dot rotate 1 and now if we print it we will see that this will rotate all elements one place to the right I can also say for example T dot rotate 2 and then this will rotate all elements two places to the right or if I want to rotate to the left side and I will give a negative number here so if I say d dot rotate minus 1 then all our elements will rotate one place to the left and yeah that's all I wanted to show you about collections I hope you enjoyed this tutorial and see you in the next tutorial where we will talk about the itertools module in Python
Original Description
Collections in Python - Advanced Python 06 - Programming Tutorial
In this Python Advanced Tutorial, we will be learning about the collections module in Python. The collections module implements special container datatypes and provides alternatives with some additional functionality compared to the general built-in containers, like dict, list, or tuple.
~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~
✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer *
📚 Get my FREE NumPy Handbook:
https://www.python-engineer.com/numpybook
📓 Notebooks available on Patreon:
https://www.patreon.com/patrickloeber
⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN
We will be talking about 5 different types from the collections module: The Counter, the namedtuple, the OrderedDict, the defaultdict, and the deque.
A written Tutorial can be found here:
https://www.python-engineer.com/courses/advancedpython/06-collections/
You can find me here:
Website: https://www.python-engineer.com
Twitter: https://twitter.com/patloeber
GitHub: https://github.com/patrickloeber
#Python
----------------------------------------------------------------------------------------------------------
* This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Patrick Loeber · Patrick Loeber · 6 of 60
1
2
3
4
5
▶
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
PyTorch Tutorial 01 - Installation
Patrick Loeber
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber
🎓
Tutor Explanation
DeepCamp AI