0% found this document useful (0 votes)
5 views2 pages

Python_Usage and Differences of List, Tuple, Set, and Dictionary

The document provides an overview of Python's List, Tuple, Set, and Dictionary data structures, highlighting their characteristics and differences. Lists are mutable and ordered, tuples are immutable and can be used as dictionary keys, sets contain unique elements, and dictionaries store key-value pairs for fast lookups. Examples illustrate the usage of each data structure, including operations like filtering, sorting, and grouping.

Uploaded by

abhidoc1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Python_Usage and Differences of List, Tuple, Set, and Dictionary

The document provides an overview of Python's List, Tuple, Set, and Dictionary data structures, highlighting their characteristics and differences. Lists are mutable and ordered, tuples are immutable and can be used as dictionary keys, sets contain unique elements, and dictionaries store key-value pairs for fast lookups. Examples illustrate the usage of each data structure, including operations like filtering, sorting, and grouping.

Uploaded by

abhidoc1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Prepared by Satyajit Mukherjee Mobile-9433123487

Python: Usage and Differences of


List, Tuple, Set, and Dictionary
1. Python List: Mutable and Ordered

Context: Lists in Python function as dynamic arrays and are ideal for storing sequences of
items that might change. Lists support duplicate elements, indexing, and slicing.

Example: Flattening and Filtering a Nested List

nested_list = [[1, 2], [3, 4], [5, 6]]


flattened = [num for sublist in nested_list for num in sublist if num %
2 == 0]
print("Flattened & Filtered Even:", flattened)

Example: Case-insensitive Sorting


names = ['alice', 'Bob', 'claire', 'david']
names.sort(key=lambda x: x.lower())
print("Case-insensitive sorted list:", names)

2. Python Tuple: Immutable and Ordered

Context: Tuples are fixed-size, immutable sequences. Useful for protecting data integrity
and can be used as dictionary keys.

Example: Tuples as Dictionary Keys

locations = {
('New York', 10001): "East Coast",
('Los Angeles', 90001): "West Coast"
}
print("Location Info:", locations[('New York', 10001)])

Example: Tuple Unpacking in a Loop


data = [(1, 'apple'), (2, 'banana'), (3, 'cherry')]
for idx, fruit in data:
print(f"ID: {idx}, Fruit: {fruit}")

3. Python Set: Unordered and Unique

Context: Sets are collections of unique elements. Ideal for operations involving union,
intersection, and membership testing.

Example: Set Operations

skills_user1 = {"Python", "Java", "SQL"}


skills_user2 = {"Python", "C++", "NoSQL"}

pg. 1
Prepared by Satyajit Mukherjee Mobile-9433123487

common_skills = skills_user1 & skills_user2


all_skills = skills_user1 | skills_user2
exclusive_skills = skills_user1 ^ skills_user2
print("Common:", common_skills)
print("All:", all_skills)
print("Exclusive to either:", exclusive_skills)

4. Python Dictionary: Key-Value Mapping

Context: Dictionaries provide a key-value store. Ideal for fast lookups, grouping, and
structured data.

Example: Grouping Using defaultdict

from collections import defaultdict


students = [
("Alice", "Math"), ("Bob", "Science"), ("Alice", "Science"),
("David", "Math")
]
grouped = defaultdict(list)
for name, subject in students:
grouped[name].append(subject)
print("Grouped Subjects:", dict(grouped))

Example: Dictionary Comprehension

grades = {'Alice': 85, 'Bob': 92, 'Clair': 78}


passed = {name: score for name, score in grades.items() if score >= 80}
print("Passed Students:", passed)

Summary of Differences: List, Tuple, Set, Dictionary

Feature List Tuple Set Dictionary


Syntax [] () {} {key: value}
Ordered Yes Yes No Yes (insertion
ordered)
Mutable Yes No Yes Yes
Duplicates Allowed Allowed Not allowed Keys not
duplicated,
values can be
Hashable No Yes (if Yes (only Keys must be
elements are immutable hashable
hashable) items)
Best Use Dynamic Fixed data Unique data Fast lookup
Case sequences pairs analysis and mapping

pg. 2

You might also like