Introduction to Sets, Named Tuples and Deques in Python Programming

Introduction to Sets, Named Tuples and Deques in Python Programming


Published at - Nov 03, 2021

Introduction to Sets, Named Tuples and Deques in Python Programming

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.

Sets

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>

Named Tuples

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[1] > 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.

Deques

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.

Summary

  1. 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.

  2. 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





About author

Harendra
Harendra Kanojiya

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.



Related Posts -

Major Differences between Python and R Programming Languages
Major Differences between P ...

Python vs R: Learn the major differences between Python and R programming l...



Make Your Own Chrome Browser with Python PyQt5
Make Your Own Chrome Browse ...

In this step-by-step Python coding tutorial, I will show you how to create ...



How do I connect to a MySQL Database in Python?
How do I connect to a MySQL ...

Connecting to MYSQL with Python 2 in three steps 1 &#8211; Setting Y...



An Introduction to Python map() Function
An Introduction to Python m ...

Python map() function is used to execute a function on all elements of an i...





Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile