Day - 13
LISTS in Python
A.I. Powered 30 Days Python Micro Course By Satish Dhawale ( Microsoft Certified Trainer )
A list is like a bag or container where you keep
multiple items together.
• Example:
• Shopping list
• To-do list
• List of cities
• List of marks
• Technical Definition
• A list is an ordered, mutable (changeable) collection that can
store multiple values of different data types.
• [ item1, item2, item3 ]
Why Lists Are Important for Data Analysts?
Lists are the foundation of handling tabular data before Pandas.
Store Store Clean Apply
• Store multiple rows • Store names, • Clean entire • Apply
of data numbers, prices, columns using transformations
dates loops
Replace Perform Generate Represent
• Replace wrong • Perform filtering • Generate new • Represent CSV
values cleaned lists columns
Lists
Creating Accessing
Creating Lists (Basic) Accessing List Items (Indexing)
• fruits = ["Apple", "Banana", "Mango"] • cities = ["Mumbai", "Pune", "Delhi"]
• numbers = [10, 20, 30, 40] • print(cities[0]) # Mumbai
• mixed = ["Satish", 25, 85.6, True] • print(cities[1]) # Pune
• print(cities[-1]) # Delhi
Working with List
Updating List Items
• cities[1] = "Nashik"
• print(cities)
Adding Items to a List
• [Link]("Chennai")
insert() → Add at position
• [Link](1, "Hyderabad")
Removing Items from a List
• [Link]("Delhi") # Remove by value
• [Link]() # Remove last item
• [Link](1) # Remove by index
Working with List
Slicing Looping Checking
List Slicing Looping Through a Checking
• nums = [10, 20, 30, 40, List Membership
50] • for item in cities: • print("Pune" in cities)
• print(nums[1:4]) # • print(item) • print("Goa" not in cities)
[20,30,40]
• print(nums[:3]) # first 3
• print(nums[-3:]) # last 3
Practical Example – Clean City Names
raw = [" mUMbai", " DELhi ", "pune ", "CHENNAI"]
clean = []
for c in raw:
[Link]([Link]().title())
print(clean)
Replacing Wrong City Spellings
wrong = ["Mombai", "Kolkatta", "Bengluru"]
correct = []
for city in wrong:
city = [Link]("Mombai", "Mumbai") \
.replace("Kolkatta", "Kolkata") \
.replace("Bengluru", "Bengaluru")
[Link](city)
print(correct)
List of Prices → Calculate Total
prices = [500, 700, 1200, 850]
total = sum(prices)
print("Total Amount:", total)
Assignments
Basic
• Create a list of 5 fruits and print each using a loop.
• Replace any one item in the list.
Intermediate
• Clean a list of messy country names:
[" inDIA ", " usa", "jAPAn "]
• Remove wrong names and correct spelling.
Advanced
• Extract last 4 digits from employee codes:
["EMP-9001", "EMP-8765", "EMP-7777"]
• Create “cleaned_products” list by removing spaces & making names title case.