Monday, 13 April 2026

April Python Bootcamp Day 8

Day 8: Mastering Python Lists

Lists are one of the most powerful and frequently used data structures in Python. Whether you're working in Data Science, Web Development, or DSA, lists are everywhere.

In this session, we focus on understanding lists through practical usage, so you build strong logic instead of just memorizing syntax.


What is a List?

A list is a collection of multiple items stored in a single variable.

numbers = [10, 20, 30, 40, 50]

Key Properties:

  • Ordered (index-based)
  • Mutable (can be changed)
  • Allows duplicate values
  • Can store mixed data types

Basic Operations on Lists

1. Creating a List

nums = [1, 2, 3, 4, 5]

2. Accessing Elements

print(nums[0]) # First element
print(nums[-1]) # Last element

3. Adding Elements

nums.append(6) # Add at end
nums.insert(1, 100) # Insert at specific position

4. Removing Elements

nums.remove(3) # Remove specific value
nums.pop() # Remove last element

Intermediate Concepts

1. Sum of Elements

nums = [1, 2, 3, 4]
print(sum(nums))

2. Finding Maximum

print(max(nums))

3. Reversing a List

nums.reverse()

OR

nums = nums[::-1]

4. Counting Frequency

nums = [1, 2, 2, 3, 2]
print(nums.count(2))

Advanced List Handling

1. Removing Duplicates

nums = [1, 2, 2, 3, 4, 4]
unique = list(set(nums))

2. Merging Two Lists Without Duplicates

a = [1, 2, 3]
b = [3, 4, 5]

merged = list(set(a + b))

3. Second Largest Element

nums = [10, 20, 4, 45, 99]

nums = list(set(nums))
nums.sort()
print(nums[-2])

4. Flattening a Nested List

nested = [[1, 2], [3, 4], [5]]

flat = [item for sublist in nested for item in sublist]

Important Notes

  • Lists are dynamic, meaning they can grow or shrink
  • Use built-in functions for efficiency instead of manual loops
  • Avoid unnecessary nested loops when list comprehension can solve it
  • Always think in terms of time complexity (important for DSA)

Practice Questions

Basic

  • Create a list of 5 numbers and print it
  • Access first and last element
  • Add an element to a list
  • Remove an element from a list

Intermediate

  • Find the sum of all elements in a list
  • Find the largest element
  • Reverse a list
  • Count frequency of an element

Advanced

  • Remove duplicates from a list
  • Merge two lists without duplicates
  • Find second largest number
  • Flatten a nested list

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (242) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (4) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (22) data management (15) Data Science (342) Data Strucures (16) Deep Learning (148) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (281) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1295) Python Coding Challenge (1128) Python Mistakes (51) Python Quiz (470) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)