Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 ?
websites = ('Tutorialspoint', 'is', 'the', 'best', 'platform', 'to', 'learn', 'new', 'skills')
print(websites)
The output of the above code is ?
('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 ordered 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 ?
company_info = {
"companyname": "Tutorialspoint",
"tagline" : "simplyeasylearning",
}
print(company_info)
The output of the above code is ?
{'companyname': 'Tutorialspoint', 'tagline': 'simplyeasylearning'}
Accessing Elements
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]])
The output of the above code is ?
First name in tuple: Amit Employee Designation: Trainee Engineer
Key Differences
The following table summarizes the main differences between a tuple and a dictionary in Python ?
| Aspect | Tuple | Dictionary |
|---|---|---|
| Structure | Sequence of values | Key-value pairs |
| Syntax | Parentheses ( ) | Curly brackets { } |
| Mutability | Immutable | Mutable |
| Ordering | Ordered | Ordered (Python 3.7+) |
| Access Method | By index (0, 1, 2...) | By key |
| Creation Function | tuple() | dict() |
| Empty Structure | ( ) | { } |
| Duplicates | Allows duplicate values | Keys must be unique |
Conclusion
Tuples are immutable sequences accessed by index, making them ideal for fixed data collections. Dictionaries are mutable key-value mappings that provide fast lookups by unique keys, perfect for associative data relationships.
