
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between a python tuple and a dictionary?
Python offers many built-in data structures like lists, tuples, sets, and dictionaries, which are used to store and manage data easily. In this article, we will discuss the difference between a Python tuple and a dictionary.
Tuple
Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they are immutable. We can't change the elements of a tuple, but we can execute a variety of actions on them, such as count, index, type, etc.
Tuples are created in Python by placing a sequence of values separated by a 'comma', with or without the use of parentheses for data grouping. Tuples can have any number of elements and any type of data (like strings, integers, lists, etc.).
Example
In the below example, we will look at how to create a tuple -
tuple = ('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills') print(tuple)
Output
When you run the program, it will show this output -
('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills')
Dictionary
A dictionary is a Python container that maintains mappings of unique keys to values in an unordered and mutable manner. Data values are stored in key-value pairs using dictionaries. Dictionaries are defined using curly brackets and have keys and values.
Example
Following is an example to create a dictionary -
thisdict = { "companyname": "Tutorialspoint", "tagline" : "simplyeasylearning", } print(thisdict)
Output
After running the program, you will get this result -
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Comparing Tuple and Dictionary
The difference between a tuple and a dictionary in Python lies in how they store and retrieve data. We can access the elements of a tuple using an index, while we can access the elements of a dictionary using keys.
Example
In the following example, we have declared a tuple called 'names' and a dictionary called 'designation'. We will access the first name from the tuple using the index value and the designation of that employee using a key.
# Tuple example names = ("Amit", "Ankit", "Aakriti") # Access by index print("First name in tuple:", names[0]) # Dictionary example designation = { "Amit": "Trainee Engineer", "Ankit": "Project Manager", "Aakriti": "Technical Lead" } # Access by key print("Employee Designation:", designation[names[0]])
Output
This output will be displayed when the program runs -
First name in tuple: Amit Employee Designation: Trainee Engineer
Differences between a tuple and a dictionary
The following are the main differences between a tuple and a dictionary in Python.
Tuple | Dictionary |
---|---|
A tuple is a non-homogeneous data structure that can hold a single row as well as several rows and columns. | Dictionary is a non-homogeneous data structure that contains key-value pairs. |
Tuples are represented by brackets (). | Dictionaries are represented by curly brackets {}. |
Tuples are immutable, i.e, we can not make changes. | Dictionaries are mutable, and keys do not allow duplicates. |
A tuple is ordered. | Dictionary is ordered (Python 3.7 and above). |
A tuple can be created using the tuple() function. | Dictionary can be created using the dict() function. |
Creating an empty Tuple: () | Creating an empty dictionary: {} |
As tuples are immutable, the reverse() method is not defined in them. | Because the dictionary's entries are in the form of key-value pairs, the elements cannot be reversed. |
Example: ('Tutorialspoint', 'simple', 'easy learning') | Example: {'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'} |