Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Learn Python Tuples Data Structure – Part 2 - DesignLinux

May 29 2020
designlinux 0 Comments

In this Part 2 of Python Data Structure series, we will be discussing what is a tuple, how it differs from other data structure in python, how to create, delete tuple objects and methods of tuple objects and how tuple differs from the list.

  • Python tuples are similar to list data structure but the main difference between list and tuple is, the list is mutable type while tuples are immutable type.
  • Tuples support indexing (both positive and negative indexing) and slicing operations.
  • Tuples, in general, will be used to store heterogeneous data.
  • Compared to list iterating over tuple is fast.
  • Tuples can be used as a “key” to dictionary objects since they are hashable.
  • We can also store mutable data type inside the tuple like list, set, etc.
  • Elements of tuples cannot be modified unless the element is of mutable type.
  • Tuples are represented using parenthesis "()".

Construct Tuple Object

Similar to list tuple also has 2 ways of constructing the object.

  1. Tuple constructor method “tuple()”.
  2. Parenthesis with values separated by a comma.
Construct Tuple Object

Construct Tuple Object

NOTE: You can create empty tuple or tuple with many values, but when you are creating tuple with one value you should add a trailing comma to it otherwise it will not be considered as a tuple object.

Tuple with Commas

Tuple with Commas

You can also create tuple without parenthesis by assigning multiple values to a variable separated by commas and that will be converted to tuple object. This is called as tuple packing.

Tuple Packing

Tuple Packing

Tuple Indexing and Slicing

Similar to the list, tuple also supports indexing and slicing operation.

Each item in the tuple is assigned to an index position starting from (0) and negative index position starting from (-1). We can access the index position to get the value or even we can update the tuple item if it is only of mutable types like a list or set.

We can also use slicing to access the items in the list. Slicing allows us to access a range of items by defining the starting, ending, step parameters.

Tuples Indexing

Tuples Indexing

Tuples Indexing & Slicing

Tuples Indexing & Slicing

Tuple being an immutable type you cannot modify or remove elements from tuple but we can modify or delete a mutable element that is present inside a tuple.

Consider the example:

b = (1,2,3,'Leo',[12,13,14],(1.1,2.2))

There is a mutable object list inside tuple b at index 4. Now we can modify or delete the elements of this list.

Modify Mutable Elements

Modify Mutable Elements

Tuple Methods

Use built-in “dir()” function to access the methods and attributes for tuple objects.

Tuple Methods

Tuple Methods

count(x) method – Returns the number of times x is present in the tuple.

Tuple Count Method

Tuple Count Method

Index(x) method – Returns the first index position of x.

Tuple Index Method

Tuple Index Method

Similar to the list we can combine two tuple objects into a single object using “+” operator.

Combine two tuple objects

Combine two tuple objects

Removing and Deleting Tuple Object

Tuple being an immutable type we cannot remove elements from it. We can delete the tuple object from the namespace using the built-in keyword “del”.

Delete Tuple Object

Delete Tuple Object

Summary

In this article, you have seen what is a tuple, how tuple is constructed, how to use indexing and slicing operations, tuple methods, etc. Tuple being an immutable type can be used as “key” to dictionary objects. Iterating through a tuple is faster compared to the list. It is best to use tuple when we have our data to remain constant throughout our program.

In the next article, we will be taking a look at another built-in data structure dictionary. Till then, you can read more about Tuples here.

Sharing is Caring…
Share on FacebookShare on TwitterShare on LinkedinShare on Reddit

Related

Tags: Python Data Structure

How to Install LEMP Server on CentOS 8

Prev Post

How to Install Google Chrome on Kali Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 387
  • 655,269

DesignLinux.com © All rights reserved

Go to mobile version