0% found this document useful (0 votes)
3 views7 pages

Python DataStructures Analysis

The document explains the differences between Python data structures: List, Tuple, Dictionary, and Set, highlighting their specific purposes in data analysis. Lists are ordered and mutable, Tuples are immutable, Dictionaries store data as key-value pairs, and Sets contain unique, unordered elements. Each structure has distinct use cases in organizing and analyzing data effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Python DataStructures Analysis

The document explains the differences between Python data structures: List, Tuple, Dictionary, and Set, highlighting their specific purposes in data analysis. Lists are ordered and mutable, Tuples are immutable, Dictionaries store data as key-value pairs, and Sets contain unique, unordered elements. Each structure has distinct use cases in organizing and analyzing data effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Difference Between Python Data Structures: List, Tuple, Dictionary, and Set (From

Analysis Perspective)
In Python, data structures such as List, Tuple, Dictionary, and Set are used for organizing
and analyzing data. Each structure serves a specific purpose in data analysis, from storing
raw values to removing duplicates or mapping relationships.

1. List – For Ordered and Changeable Data


A List is an ordered, mutable (changeable) collection that allows duplicates. In analysis,
lists are used for storing raw data such as numerical or categorical values before
converting them into more advanced structures like pandas DataFrames.

Example:

sales = [120, 150, 130, 150, 170]


avg_sales = sum(sales) / len(sales)
[Link](200)

print("Sales List:", sales)


print("Average Sales:", avg_sales)

✅ Use in Analysis:
• Storing sequential data (e.g., sales, temperatures)
• Quick calculations or iteration
• Can easily convert to NumPy arrays or pandas DataFrames

2. Tuple – For Fixed or Immutable Data


A Tuple is similar to a list but immutable. It is used for fixed data such as coordinates or
constant values.

Example:

location = (12.97, 77.59)


print("Latitude:", location[0])
print("Longitude:", location[1])

✅ Use in Analysis:
• Representing constant data like coordinates
• Used as dictionary keys (hashable)
• Ensures data integrity
3. Dictionary – For Mapped or Structured Data
A Dictionary stores data as key–value pairs. It is ideal for structured or labeled data,
where each value is accessed by a key.

Example:

student = {"name": "Anusha", "age": 22, "marks": [85, 90, 78]}


print("Student Name:", student["name"])
print("Average Marks:", sum(student["marks"]) / len(student["marks"]))
student["grade"] = "A"
print(student)

✅ Use in Analysis:
• Storing structured or labeled data
• Mapping and quick lookups
• Foundation for pandas DataFrames

4. Set – For Unique and Unordered Data


A Set is an unordered collection of unique elements. It is commonly used for removing
duplicates or comparing datasets.

Example:

customers = ["Asha", "Ravi", "Asha", "Meena", "Ravi"]


unique_customers = set(customers)
print("Unique Customers:", unique_customers)

north_region = {"Asha", "Ravi", "Meena"}


south_region = {"Ravi", "Kiran"}
common_customers = north_region & south_region
print("Common Customers:", common_customers)

✅ Use in Analysis:
• Removing duplicate data
• Performing set operations (union, intersection)
• Comparing distinct groups in datasets

Summary Table (From Analysis Perspective)


Data Ordered Mutable Allows Use in Example
Structure Duplicates Analysis
List [ ] Yes Yes Yes Store [120, 150,
sequential 130]
raw data

Tuple ( ) Yes No Yes Fixed data (12.97,


(e.g., 77.59)
coordinates)

Dictionary { Yes (3.7+) Yes No (keys Labeled {'Name':


} unique) structured 'Asha',
data 'Age': 22}

Set { } No Yes No Unique or {'Asha',


distinct data 'Ravi',
'Meena'}

CODE for LIST, Tuple, Dictionary & sets

List

# List: ordered, mutable, allows duplicates

fruits = ["apple", "banana", "cherry", "apple"]

print("List:", fruits)

# Access elements

print("First element:", fruits[0])

# Modify list

[Link]("mango")

print("After append:", fruits)

OUTPUT/ANSWER
List: ['apple', 'banana', 'cherry', 'apple']
First element: apple
After append: ['apple', 'banana', 'cherry', 'apple', 'mango']

TUPLE

# Tuple: ordered, immutable, allows duplicates


numbers = (10, 20, 30, 10)
print("Tuple:", numbers)

# Access element
print("Second element:", numbers[1])

# numbers[1] = 25 ❌ (Error - tuples cannot be changed)

OUTPUT/ANSWER
Tuple: (10, 20, 30, 10)
Second element: 20

DICTIONARY

# Dictionary: key-value pairs, mutable, keys unique


student = {"name": "Anusha", "age": 22, "course": "Python"}
print("Dictionary:", student)

# Access value by key


print("Student name:", student["name"])

# Add or modify key-value


student["age"] = 23
student["grade"] = "A"
print("Updated Dictionary:", student)

OUTPUT/ANSWER

Dictionary: {'name': 'Anusha', 'age': 22, 'course': 'Python'}


Student name: Anusha
Updated Dictionary: {'name': 'Anusha', 'age': 23, 'course':
'Python', 'grade': 'A'}

SETS

# Set: unordered, unique elements only


colors = {"red", "green", "blue", "red"}
print("Set:", colors) # Duplicate 'red' removed

# Add element
[Link]("yellow")
print("After adding:", colors)

# Set operations
primary = {"red", "blue", "yellow"}
print("Intersection:", colors & primary)

OUTPUT/ANSWER

Set: {'blue', 'green', 'red'}


After adding: {'yellow', 'blue', 'green', 'red'}
Intersection: {'yellow', 'blue', 'red'}

HOMEWORK

LIST

# List of sales amounts


sales = [120, 150, 130, 150, 170]

# Access and modify


avg_sales = sum(sales) / len(sales)
[Link](200) # Add new data point

print("Sales List:", sales)


print("Average Sales:", avg_sales)

OUTPUT/ANSWER
Sales List: [120, 150, 130, 150, 170, 200]
Average Sales: 144.0

TUPLE
# Tuple of coordinates
location = (12.97, 77.59) # latitude, longitude

# Accessing elements
print("Latitude:", location[0])
print("Longitude:", location[1])

# location[0] = 13.0 ❌ Error: Tuples cannot be changed

OUTPUT/ANSWER

Latitude: 12.97
Longitude: 77.59

DICTIONARY

# Dictionary of student data


student = {"name": "Anusha", "age": 22, "marks": [85, 90, 78]}

# Access data
print("Student Name:", student["name"])
print("Average Marks:", sum(student["marks"]) /
len(student["marks"]))

# Add new key-value pair


student["grade"] = "A"
print(student)

OUTPUT/ANSWER
Student Name: Anusha
Average Marks: 84.33333333333333
{'name': 'Anusha', 'age': 22, 'marks': [85, 90, 78], 'grade': 'A'}

SETS

# Raw customer data with duplicates


customers = ["Asha", "Ravi", "Asha", "Meena", "Ravi"]

# Remove duplicates using set


unique_customers = set(customers)
print("Unique Customers:", unique_customers)
# Compare two sets
north_region = {"Asha", "Ravi", "Meena"}
south_region = {"Ravi", "Kiran"}

common_customers = north_region & south_region


print("Common Customers:", common_customers)

OUTPUT/ANSWER

Unique Customers: {'Meena', 'Ravi', 'Asha'}


Common Customers: {'Ravi'}

You might also like