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.


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="" 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="" frameborder=0></iframe>

We can also use the class to convert any iterable to a set.

<iframe src="" 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="" frameborder=0></iframe>

Sets, like lists, contain a delete function, but instead of append, they have an add method.

<iframe src="" 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="" frameborder=0></iframe>

Named Tuples

It’s tempting to create a class that’s nothing more than a collection of data.

<iframe src="" 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 = ('', 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="" 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="" 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="" 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.


  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

About author

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