Notes
Categories

List in Python [ English ]

< Prev Next >

List in Python

1. Introduction

A list in Python is a collection of items stored in a single variable. It is one of the most versatile and widely used data structures.

Lists are:


2. Creating a List

Syntax

list_name = [elements]

Examples

numbers = [1, 2, 3, 4]
names = ["Amit", "Rahul", "Priya"]
mixed = [10, "Python", 3.5]

3. Accessing Elements

Indexing (starts from 0)

numbers = [10, 20, 30]

print(numbers[0])
print(numbers[2])

Output

10
30

Negative Indexing

print(numbers[-1])

Output

30

4. Slicing

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

print(numbers[1:4])

Output

[2, 3, 4]

5. Modifying List Elements

numbers = [1, 2, 3]

numbers[1] = 10
print(numbers)

Output

[1, 10, 3]

6. Adding Elements

append() → adds at end

numbers = [1, 2]
numbers.append(3)

insert() → adds at specific position

numbers.insert(1, 5)

extend() → adds multiple elements

numbers.extend([6, 7])

7. Removing Elements

remove() → removes specific value

numbers.remove(5)

pop() → removes last element

numbers.pop()

del → deletes element by index

del numbers[0]

8. List Operations

Concatenation

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

print(a + b)

Repetition

print([1, 2] * 2)

Membership

print(2 in [1, 2, 3])

9. Looping Through List

numbers = [1, 2, 3]

for i in numbers:
    print(i)

10. Nested List

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

print(matrix[0][1])

Output

2

11. Important List Functions

Function Description
len() Returns number of elements
max() Returns maximum value
min() Returns minimum value
sum() Returns sum of elements

12. Key Features


13. Summary

< Prev Next >