Python List Tutorial: Easy Explanation with Examples

 In Python programming, we often need to store several values together — such as numbers, names, or even mixed data.

Creating separate variables for each value becomes difficult, especially when the data grows.

To solve this, Python provides a powerful and flexible data structure called a List.

A list allows us to:

group multiple values in a single variable

access elements easily using indexes

add, update, or remove items whenever needed

work with dynamic data that changes over time

Because of this flexibility, lists are one of the most widely used data types in Python, especially for beginners learning data handling.


Python List:

1. What is a List?

A list in Python is an ordered collection of items.

It can store numbers, strings, floats, or mixed data types.

Example:



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

numbers = [10, 20, 30]

mixed = [10, "apple", 5.5]

Lists are mutable, meaning we can change/modify them.

2. Create a List



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

3. Access List Items

Use index (starts from 0):

numbers = [10, 20, 30, 40]

print(numbers[0]) # 10

print(numbers[2]) # 30

4. Update List Values

numbers = [10, 20, 30]

numbers[1] = 100

print(numbers) # [10, 100, 30]

5. Adding Items to List

A) append() → adds at end

fruits = ["apple", "banana"]

fruits.append("mango")

print(fruits) # ['apple', 'banana', 'mango']

B) insert() → add at a specific index


fruits = ["apple", "banana"]

fruits.insert(1, "orange")

print(fruits) # ['apple', 'orange', 'banana']

C) extend() → add multiple items


a = [1, 2]

b = [3, 4, 5]

a.extend(b)

print(a) # [1, 2, 3, 4, 5]

6. Removing Items

A) remove() → remove by value


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

fruits.remove("banana")

print(fruits) # ['apple', 'orange']

B) pop() → remove by index

numbers = [10, 20, 30, 40]

numbers.pop(2)

print(numbers) # [10, 20, 40]

C) del → delete element by index

numbers = [1, 2, 3]

del numbers[0]

print(numbers) # [2, 3]

D) clear() → remove all items

numbers = [1, 2, 3]

numbers.clear()

print(numbers) # []

7. Copying a List

A) copy()

a = [1, 2, 3]

b = a.copy()

print(b) # [1, 2, 3]

B) list()

a = [10, 20, 30]

b = list(a)

print(b)

8. List Slicing

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

print(numbers[1:4]) # [20, 30, 40]

print(numbers[:3]) # [10, 20, 30]

print(numbers[2:]) # [30, 40, 50]

9. Looping Through a List

Method 1: for loop

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

for f in fruits:

    print(f)

Method 2: Using index

for i in range(len(fruits)):

    print(fruits[i])

10. List Comprehension

List Comprehension = Create new lists in one line.

Example :

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

square = [x*x for x in numbers]

print(square) # [1, 4, 9, 16, 25]

Example 2: Filter

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

even = [x for x in nums if x % 2 == 0]

print(even) # [2, 4, 6]

11. Types of Lists

Homogeneous List

nums = [1, 2, 3, 4]

Heterogeneous List

details = ["Sai", 25, 5.7]

Nested List

matrix = [[1, 2], [3, 4]]

12. Useful List Functions

Function

Purpose

Example

len()

Length

len([1,2,3]) → 3

min()

Minimum

min([4,1,9]) → 1

max()

Maximum

max([4,1,9]) → 9

sum()

Sum of numbers

sum([1,2,3]) → 6

13. Final Example (All Operations Together)


numbers = [10, 20, 30]


numbers.append(40)

numbers.insert(1, 15)

numbers.remove(30)


print(numbers) # [10, 15, 20, 40]


square = [x*x for x in numbers]

print(square) # [100, 225, 400, 1600]


That’s all about Python Lists — tomorrow we’ll learn Tuples in a simple and easy way.”


Comments

Popular posts from this blog

Python Programming: Definition, Advantages, and Applications

Python Data Types Explained with Examples.