Examples of and explanation on Sets, Named tuples, and Deques.
We’re all familiar with lists, tuples, and dictionaries. They’re standard Python data types, and there’s nothing wrong with them. We’ll discover new data types in this chapter, which will make things easier. You can always use lists and dictionaries for everything, but these data types can help you out a lot.
Many of the objects in this tutorial aren’t objects, but they act as if they are. We’ll learn about numerous classes that behave like dictionaries, for example. They aren’t dictionaries, but they may be used in the same way. Duck-typing is the name for this programming approach.
Let’s pretend we have software that records people’s names. We can keep track of the names in a list, and adding a new one is as simple as appending to it. Lists remember their order, and the same item can be added several times.
<iframe src="https://medium.com/media/d1242b2a3c6bae7e7f59bdd351806edb" frameborder=0></iframe>
This is normally what we require, but it is not always the case. Sometimes all we want to do is store a bunch of stuff. We don’t need the same thing again, and the sequence is irrelevant to us.
This is when the sets come in handy. They’re similar to lists with no order or duplicates, or dictionary keys without values. We can make a set in the same way as we can make a dictionary, but without :.
<iframe src="https://medium.com/media/613e4e639f8e27df826482e36ba5d2d0" frameborder=0></iframe>
We can also use the class to convert any iterable to a set.
<iframe src="https://medium.com/media/37e96b811e99084cd833065f2800bf17" frameborder=0></iframe>
Because sets don’t include duplicates or maintain track of their order, we lost one l and the set ended up in a different order when we did set('hello').
Note that is a dictionary since dictionaries are more commonly used than sets, thus we’ll need set() to make an empty set.
<iframe src="https://medium.com/media/c2be79742192daf0fa3519cd44b0a7df" frameborder=0></iframe>
Sets, like lists, contain a delete function, but instead of append, they have an add method.
<iframe src="https://medium.com/media/e45271d9ccdd3657333898852a74d01b" frameborder=0></iframe>
That is the tedious part. Now let’s take a look at some of the things we can do with useful sets:
<iframe src="https://medium.com/media/6bb5345088e40b4c7157b1d6e517ff81" frameborder=0></iframe>
It’s tempting to create a class that’s nothing more than a collection of data.
<iframe src="https://medium.com/media/676adb3fe415379ba44a44d76ac18734" frameborder=0></iframe>
This is the type of class you should avoid. Because this class just contains one function, it isn’t required to be a class. Instead, we might use a tuple:
github = ('https://github.com/', 2008, True)
The issue is that when someone reads our code and sees something like website > 2010, it doesn’t make as much sense as something like website.founding year > 2010.
The collections.namedtuple is beneficial in situations like this:
<iframe src="https://medium.com/media/773095202827b165ba58489b40cffa68" frameborder=0></iframe>
As you can see, our GitHub operates like a tuple, but things like github.founding year work as well, and GitHub looks beautiful on the >>> prompt.
To comprehend deques, we must first learn about a list approach that I haven’t discussed previously. It’s known as pop, and it operates as follows:
<iframe src="https://medium.com/media/67792db7d4e0d2108c1e6b089ea9d805" frameborder=0></iframe>
When we pop from the list, it shortens by one item from the end, and we also get the deleted item back. So, using ‘append’, we can add an item to the end of a list, and using ‘pop’, we can remove an item from the end.
These things can also be done at the start of a list, but lists were not supposed to be used that way, and it would be slow if our list was long. Appending and popping from both ends is simple and quick with the ‘collections.deque’ class. It functions similarly to lists, except with the addition of ‘appendleft’ and ‘popleft’ methods.
<iframe src="https://medium.com/media/457f61edbcf5fe1d39ca87a814e4e7b5" frameborder=0></iframe>
The deque functions similarly to lists, and if we require a list instead of a deque for whatever reason, we may use a list(names).
Queues are frequently used with deques. It means that objects are always popped from one end and added to the other.
Duck typing refers to the need for some behaviour rather than a specific type. Instead of creating a function that accepts a list, we might create one that accepts any iterable.
The collections module and sets are really useful. Make use of them.
I hope you found this lesson useful in learning about Sets, Named Tuples, and Deques.
If you appreciated this fast instruction, please clap, and don’t forget to follow me to read more posts like this.
Thank you for taking the time to read this.
More content at plainenglish.io
Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.