Python Data Structure
Python Data Structure
as lists, tuples, sets, and dictionaries that store and organize the data
efficiently. In this article, we will learn the difference between them and
their applications in Python.
Difference between List, Tuple, Set, and Dictionary
The following table shows the difference between various Python built-in
data structures.
List Tuple Set Dictionary
A list is a non-
A Tuple is a non-
homogeneous The set data A dictionary is
homogeneous data
data structure structure is non- also a non-
structure that
that stores the homogeneous homogeneous
stores elements in
elements in but stores the data structure
columns of a single
columns of a elements in a that stores key-
row or multiple
single row or single row. value pairs.
rows.
multiple rows.
Dictionary is
List is ordered Tuple is ordered Set is unordered ordered (Python
3.7 and above)
Python List
Python Lists are just like dynamic-sized arrays, declared in other
languages (vector in C++ and ArrayList in Java). Lists need not be
homogeneous always which makes it the most powerful tool in Python.
Here are five important applications of Python lists:
1. Data Storage and Manipulation
Lists are great for holding collections of data, whether all of the same type
(like all integers) or mixed types (e.g., integers, strings). You
can add, remove, and update elements easily.
Example:
1
a = [23, 45, 12, 67, 34]
2
a.append(89)
3
a.remove(45)
4
print(a)
Output
[23, 12, 67, 34, 89]
Output
b
Explanation: This behaves like a stack. Items are pushed using append(),
and the last item 'b' is popped from the stack.
3. Iteration and Data Processing
Lists are iterable and perfect for processing data, especially in loops
where you perform calculations or transformations.
Example:
1
a = [1, 2, 3, 4, 5]
2
t = sum(a)
3
print(f"Total: {t}")
Output
Total: 15
Explanation: We use Python’s built-in sum() to add all numbers in the list
and print the total.
4. Dynamic Arrays
Lists can grow or shrink in size, which is useful when collecting data
dynamically such as user inputs or computation results.
Example:
1
s = []
2
for i in range(10):
3
s.append(i * i)
4
print(s)
Output
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Explanation: We create a list of squares from 0 to 9 dynamically using a
loop and store the results in the list.
5. Storing and Processing Strings
Lists can be used to manipulate strings for tasks like tokenization, filtering,
or formatting.
Example:
1
s = "Subtle art of not giving a bug"
2
w = s.split()
3
for word in w:
4
print(word.upper())
Output
SUBTLE
ART
OF
NOT
GIVING
A
BUG
Explanation: We split a sentence into words using split() and convert each
word to uppercase using a loop.
Python Tuple
A Tuple is a collection of Python objects separated by commas. In some
ways, a tuple is similar to a list in terms of indexing, nested objects, and
repetition but a tuple is immutable, unlike lists that are mutable.
1. Immutable Data Storage
Tuples are used when data should not be changed, such as coordinates
or constants.
Example:
1
p= (10, 20)
2
print(p)
Output
(10, 20)
2. Dictionary Keys
Tuples are hashable and can be used as dictionary keys, unlike lists.
Example:
1
p={
2
("Paris", "France"): "Eiffel Tower",
3
("New York", "USA"): "Statue of Liberty"
4
}
5
print(p[("Paris", "France")])
Output
Eiffel Tower
1
p = ("Prajjwal", 22, "prajwal@gfg.org")
2
print(p)
Output
('Prajjwal', 22, 'prajwal@gfg.org')
Python Set
A Python Set is an unordered collection data type that is iterable, mutable
and has no duplicate elements. Python’s set class represents the
mathematical notion of a set.
1. Removing Duplicates from a Set
When you need to eliminate duplicate elements from a list, converting the
list to a set is a quick and efficient way to do so.
Example:
1
a = [1, 2, 2, 3, 4, 4, 5]
2
u = set(a)
3
print(u)
Output
{1, 2, 3, 4, 5}
2. Set Operations
Sets are ideal for performing mathematical operations
like union, intersection, and difference, which are useful in fields like data
analysis, database management, and computational biology.
Example:
1
a = {1, 2, 3, 4}
2
b = {3, 4, 5, 6}
3
print("Union:", a | b)
4
print("Intersection:", a & b)
5
print("Difference:", a - b)
Output
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference: {1, 2}
Explanation: We perform union (|), intersection (&), and difference (-
) between two sets.
3. Membership Testing
Checking for the existence of an element in a collection is very efficient
with sets due to their underlying hash table implementation. This makes
sets a good choice for membership testing in scenarios like checking if an
item is in a list of prohibited items.
Example:
1
ban = {"knife", "gun", "drugs"}
2
i = "knife"
3
if i in ban:
4
print(f"{i} is prohibited.")
Output
knife is prohibited.
Explanation: We check if a given item is in the banned set. Membership
testing in sets is fast.
4. Finding Common Elements
Sets are highly efficient for finding common elements between two or
more collections. This is useful in scenarios like finding common interests,
mutual friends, or common values between datasets.
Example:
1
a_f = {"Aryan", "Anurag", "Vishakshi"}
2
b_f = {"Prajjwal", "Brijkant","Aryan", "Kareena"}
3
comm = a_f & b_f
4
print(comm)
Output
{'Aryan'}
Explanation: We use intersection (&) to find common friends between two
sets.
5. Handling Data in Multi-Set Scenarios
Sets are useful when dealing with multiple data sources where you need
to ensure the uniqueness of the combined dataset or identify overlaps
between different data sources.
Example:
1
d1 = {"apple", "banana", "cherry"}
2
d2 = {"banana", "cherry", "date"}
3
comb = d1 | d2
4
over = d1 & d2
5
print("Combined:", comb)
6
print("Overlap:", over)
Output
Combined: {'apple', 'cherry', 'banana', 'date'}
Overlap: {'cherry', 'banana'}
Explanation: We merge two sets using union (|) and find shared items
using intersection (&).
Python Dictionary
Dictionary in Python is an ordered (since Py 3.7) collection of data values,
used to store data values like a map, which, unlike other Data Types that
hold only a single value as an element. Dictionaries store data in key:
value pairs. They are ideal for fast lookups and representing structured
data. Here are five important applications of Python dictionaries:
1. Database Record Representation
Dictionaries are often used to represent database records where
each key-value pair corresponds to a column and its associated value.
This makes it easy to access and manipulate data based on field names.
Example:
1
rec = {
2
'id': 1,
3
'name': 'Prajjwal',
4
'email': 'prajjwal@example.com',
5
'age': 30
6
}
7
print(rec['name'])
Output
Prajjwal
1
i = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
2
freq = {}
3
for item in i:
4
freq[item] = freq.get(item, 0) + 1
5
print(freq)
Output
{'apple': 3, 'banana': 2, 'orange': 1}
Explanation: We use a dictionary to count how often each fruit appears in
the list.
3. Fast Lookup Tables
Dictionaries provide O(1) average time complexity for lookups, making
them ideal for creating fast lookup tables. This can be used in applications
like caching, memoization, or mapping operations.
Example:
1
lookup = {
2
'USD': 'United States Dollar',
3
'EUR': 'Euro',
4
'JPY': 'Japanese Yen'
5
}
6
print(lookup.get('USD'))
Output
United States Dollar
1
import json
2
3
a = '{"name": "prajjwal", "age": 23, "city": "Prayagraj"}'
4
data = json.loads(a)
5
6
print(data['name'])
Output
prajjwal
Explanation: We parse a JSON string into a Python dictionary and access
a value by key.
5. Grouping Data by Keys
Dictionaries can be used to group data by certain keys, which is
particularly useful in data analysis and aggregation tasks.
Example:
1
emp = [
2
{'name': 'prajj', 'dept': 'HR'},
3
{'name': 'brij', 'dept': 'IT'},
4
{'name': 'kareena', 'dept': 'HR'},
5
{'name': 'aryan', 'dept': 'IT'}
6
]
7
8
grp = {}
9
for emp in emp:
10
dept = emp['dept']
11
grp.setdefault(dept, []).append(emp['name'])
12
13
print(grp)
Output
{'HR': ['prajj', 'kareena'], 'IT': ['brij', 'aryan']}