Nerd For Tech
Published in

Nerd For Tech

Tuples and namedtuple — Python

Photo by Pauline Bernfeld on Unsplash

I did not know tuples were more than a data structure with brackets that are usually used in return statement. Tuples are quite a powerful data structure. We’ll be looking into Tuples and Python’s factory function from library with examples to illustrate.

Tuple’s syntax is

Tuple’s syntax

Elements can be any types — integer, strings, list, etc.

Now, let’s look at tuple specific property and then look it as a data records.

Tuples are immutable, meaning once a tuple is created elements on it cannot be added or removed. The order of elements is also fixed.

elements in tuple cannot be changed, added, deleted and order is fix

We also saw that tuple variable ‘s position has meaning — for name of a country and for name of a capital city.

Given these immutable property of tuple, it’s a perfect data structure to contain data — no side effect.

Let’s see an example:

Here you see tuples contain data about countries. We can extract tuple’s values using variables to contain them. We use for naming a variable to collect more than 1 values, for eg and in the code. This process is also know as tuple unpacking.

On running the file, we get:

north america: [('United States of America', 'Washington, D.C.', 'Bald Eagle'), ('Canada', 'Otaawa', 'Canada Jay')]country: Swaziland, capital: Mbabane, Lobamba, national bird: Purple-crested turacoUSA's national bird: Bald EagleGuyana's capital: Georgetownusa: United States of America, rest info: ['Washington, D.C.', 'Bald Eagle']

Now let’s look at factory function from Python's libraries.

The combines class and tuple approach so that we can give meaningful names to the positions.

Let’s see an example:

Here we created a tuple called that has two fields — and . We can access the value of the tuple using index or through attribute. We can also unpack it using normal unpacking way.

We can get a dictionary representation of a tuple using . We can create a new instance of using values from existing instance. Here we used unpacking dictionary and unpacking positional argument way to do so.

On running the file, we get:

calculating area using indexing: 18calculating area using attribute: 18length: 9, breadth: 2dict representation: {'length': 9, 'breadth': 2}r2's initial id: 140454374705088r2's  length: 9, breadth: 3r2's id after replacing breadth value: 140454374705280r3's lenght: 9, breadth: 2

What if we want to have default values for the fields in ? We do have 2 ways to do it — prototype and using property of tuple.

First, let’s look at the prototype way:

On running the file, we get:

prototype circle's id: 140367016505888
area: 12.57, id: 140367016746416

Here while creating an instance of a called , we passed default values to the fields. Remember that once we started passing default values, it should do that to all the remaining fields that comes after it. This is to help disambiguate value assignment to variable for Python compiler. If you did not give default values, you’ll get error.

So the instance with default values will became our prototype. We can create an instance based on the prototype using method. Since tuples are immutable sequence type, see that the prototype object and new instance out of prototype have different id.

Second way is using property.

Here we used attribute to pass default values to fields in tuple. The default values gets passed from left to right, so if we had just pass 2 values they would be set to and fields .

It would work similarly in function as well:

Now let’s look at a scenario where we want to extend our existing tuple, . We would like to add field to the Circle so that this Circle works in 3D environment to become Sphere. We can do it by using attribute of tuple to get fields from 2D and add a new field. Then follow usual way of setting and access values of the tuple.

Let’s see an example:

On running the file, we get:

circle area: 28.27 at coordinate (1, 1)
sphere area: 113.10 at coordinate (1, 1, 1)

In conclusion,

  • tuples are an immutable data structure
  • tuples can work as a data record
  • we have factory function to create tuple with named fields.

I hope the article was helpful and got to know tuples better.

Congratulations on the completion and thank you for reading! 💐 My next article will be on mutability and immutability of Python’s data structures. See you then.


You can support me on Patreon!



NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store