Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Lists

Learn how to create, access, update, add, remove, search and process multiple values using Python Lists.

What is a List?

A List is one of the most commonly used data structures in Python. It allows us to store multiple values inside a single variable.

Lists are:

  • Ordered
  • Mutable
  • Allow duplicate values
  • Can contain different data types
  • Indexed from zero

Lists are widely used in real-world applications for storing collections such as students, courses, products and employee records.

Creating a List

Lists are created using square brackets [].

courses = ["Python", "Java", ".NET", "MERN"]

print(courses)

Output:

['Python', 'Java', '.NET', 'MERN']

List with Different Data Types

A Python List can contain different types of values.

student = [
    "Rahul",
    22,
    85.5,
    True
]

print(student)

Output:

['Rahul', 22, 85.5, True]

List Indexing

List elements are accessed using their index. The first element has index 0.

courses = ["Python", "Java", ".NET", "MERN"]

print(courses[0])
print(courses[1])
print(courses[2])
print(courses[3])

Output:

Python
Java
.NET
MERN

Negative Indexing

Negative indexing starts from the end of the list.

courses = ["Python", "Java", ".NET", "MERN"]

print(courses[-1])
print(courses[-2])

Output:

MERN
.NET

List Slicing

Slicing is used to extract a portion of a list.

courses = [
    "Python",
    "Java",
    ".NET",
    "MERN",
    "Data Science"
]

print(courses[1:4])

Output:

['Java', '.NET', 'MERN']

The ending index is not included in the result.

Updating List Elements

Lists are mutable, which means their elements can be changed after creation.

courses = ["Python", "Java", ".NET"]

courses[1] = "C#"

print(courses)

Output:

['Python', 'C#', '.NET']

Adding Elements using append()

The append() method adds an item at the end of the list.

courses = ["Python", "Java"]

courses.append(".NET")

print(courses)

Output:

['Python', 'Java', '.NET']

Adding Elements using insert()

The insert() method adds an item at a specific index.

courses = ["Python", ".NET"]

courses.insert(1, "Java")

print(courses)

Output:

['Python', 'Java', '.NET']

Adding Multiple Elements using extend()

The extend() method adds multiple elements from another collection.

courses = ["Python", "Java"]

more_courses = [".NET", "MERN"]

courses.extend(more_courses)

print(courses)

Output:

['Python', 'Java', '.NET', 'MERN']

Removing Elements

remove()

Removes the first matching value.

courses = ["Python", "Java", ".NET"]

courses.remove("Java")

print(courses)

Output:

['Python', '.NET']

pop()

Removes an element using its index.

courses = ["Python", "Java", ".NET"]

courses.pop(1)

print(courses)

Output:

['Python', '.NET']

clear()

Removes all elements from the list.

courses = ["Python", "Java", ".NET"]

courses.clear()

print(courses)

Output:

[]

Finding List Length

The len() function returns the number of elements in a list.

courses = ["Python", "Java", ".NET", "MERN"]

print(len(courses))

Output:

4

Searching in a List

The in operator checks whether an element exists in a list.

courses = ["Python", "Java", ".NET"]

print("Python" in courses)
print("PHP" in courses)

Output:

True
False

Counting Elements

The count() method returns how many times a value occurs.

courses = [
    "Python",
    "Java",
    "Python",
    ".NET",
    "Python"
]

print(courses.count("Python"))

Output:

3

Finding the Index

The index() method returns the position of the first matching value.

courses = ["Python", "Java", ".NET"]

print(courses.index("Java"))

Output:

1

Sorting a List

The sort() method sorts the list in ascending order by default.

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

numbers.sort()

print(numbers)

Output:

[10, 20, 30, 40, 50]

Descending Order

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

numbers.sort(reverse=True)

print(numbers)

Output:

[50, 40, 30, 20, 10]

Reversing a List

The reverse() method reverses the existing list.

courses = ["Python", "Java", ".NET"]

courses.reverse()

print(courses)

Output:

['.NET', 'Java', 'Python']

Looping Through a List

A for loop can be used to process every element of a list.

courses = ["Python", "Java", ".NET", "MERN"]

for course in courses:
    print(course)

Output:

Python
Java
.NET
MERN

List with Conditions

We can use conditions while processing list elements.

marks = [45, 78, 92, 35, 88]

for mark in marks:

    if mark >= 50:
        print(mark)

Output:

78
92
88

List Comprehension Preview

List comprehensions provide a short way to create lists.

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

squares = [number * number for number in numbers]

print(squares)

Output:

[1, 4, 9, 16, 25]

List comprehensions will be covered in detail in the Comprehensions topic.

CIIT Example 📩🤓

Suppose CIIT wants to maintain a list of students enrolled in a Python course.

students = [
    "Rahul",
    "Sneha",
    "Amit",
    "Priya"
]

students.append("Neha")

print("Total Students:", len(students))

for student in students:
    print(student)

Output:

Total Students: 5
Rahul
Sneha
Amit
Priya
Neha

Important List Methods

Method Purpose
append() Add item at the end
insert() Add item at a specific position
extend() Add multiple items
remove() Remove a specific value
pop() Remove an item using index
clear() Remove all items
count() Count occurrences
index() Find the index
sort() Sort the list
reverse() Reverse the list

Common Mistakes

  1. Remember that List indexing starts from 0.
  2. Using an index that does not exist causes an IndexError.
  3. remove() removes a value, while pop() normally removes using an index.
  4. Forgetting that List is mutable.
  5. Confusing append() with extend().

Interview Points

  • What is a List in Python?
  • Why are Lists called mutable?
  • What is the difference between append() and extend()?
  • What is the difference between remove() and pop()?
  • How do you find the length of a List?
  • How do you remove duplicate values from a List?
  • What is List slicing?

CIIT Learning Point

Lists are one of the most important Python data structures. A strong understanding of indexing, slicing, updating, adding, removing and iterating over Lists is essential before moving to real-world Python applications.

CIIT Practice Tasks

  1. Create a List containing 10 student names and display all students.
  2. Add two new students using append().
  3. Insert a student at index 2.
  4. Remove a student using remove().
  5. Find the highest and lowest number from a List of marks.
  6. Count how many times "Python" appears in a List.
  7. Create a List of numbers and print only the even numbers.

Summary :

Python Lists are ordered, mutable collections that can store multiple values. List elements are accessed using indexes and can be added, updated, removed, searched, sorted and processed using loops. Important methods include append(), insert(), extend(), remove(), pop(), clear(), count(), index(), sort() and reverse(). Lists are widely used for managing collections of data in real-world Python applications.