Python Lists – Create, Access, Modify & Use Lists

Introduction

One of the most commonly used data structures in Python is the List. Lists allow developers to store multiple values in a single variable.

Python lists are ordered, mutable (changeable), and can contain different data types. Due to their versatility, lists are widely used in web development, data analysis, automation, machine learning, and many other applications.

What is a Python List?

A Python List is a collection of items stored in a single variable. Lists are created using square brackets ([]) and can hold multiple values.

📖
Features of Python Lists:
  • Ordered collection
  • Mutable (can be modified after creation)
  • Allows duplicate values
  • Supports multiple data types
  • Indexed starting from 0
  • Dynamic size

Example:


fruits = ["Apple", "Banana", "Mango"]
print(fruits)

Output:

[‘Apple’, ‘Banana’, ‘Mango’]

In this example, fruits is a list containing three items.

Syntax

The basic syntax of a Python list is:


list_name = [item1, item2, item3]

Example:


colors = ["Red", "Green", "Blue"]

Empty List:


my_list = []

List with Different Data Types:


data = ["John", 25, True, 95.5]
print(data)

Output:

[‘John’, 25, True, 95.5]

Example

Let’s explore common list operations.

Creating a List


numbers = [10, 20, 30, 40]
print(numbers)

Output:

[10, 20, 30, 40]

Accessing List Elements

Lists use indexing starting from 0.


fruits = ["Apple", "Banana", "Mango"]
print(fruits[0])
print(fruits[2])

Output:

Apple
Mango

Modifying List Elements

Lists are mutable.


fruits = ["Apple", "Banana", "Mango"]
fruits[1] = "Orange"
print(fruits)

Output:

[‘Apple’, ‘Orange’, ‘Mango’]

Adding Items

append()


fruits = ["Apple", "Banana"]
fruits.append("Mango")
print(fruits)

Output:

[‘Apple’, ‘Banana’, ‘Mango’]

insert()


fruits = ["Apple", "Banana"]
fruits.insert(1, "Orange")
print(fruits)

Output:

[‘Apple’, ‘Orange’, ‘Banana’]

Removing Items

remove()


fruits = ["Apple", "Banana", "Mango"]
fruits.remove("Banana")
print(fruits)

Output:

[‘Apple’, ‘Mango’]

pop()


fruits = ["Apple", "Banana", "Mango"]
fruits.pop()
print(fruits)

Output:

[‘Apple’, ‘Banana’]

Looping Through a List


fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
   print(fruit)

Output:

Apple
Banana
Mango

Finding List Length


fruits = ["Apple", "Banana", "Mango"]
print(len(fruits))

Output:

3

Real-Life Example

Imagine you are building an online shopping website. A customer adds products to their cart. A Python list can store those products.


cart = ["Laptop", "Mouse", "Keyboard"]
print("Shopping Cart:")
for item in cart:
   print(item)

Output:

Shopping Cart:
Laptop
Mouse
Keyboard

Now suppose the customer adds another product:


cart.append("Headphones")
print(cart)

Output:

[‘Laptop’, ‘Mouse’, ‘Keyboard’, ‘Headphones’]

Lists are useful whenever multiple related values need to be stored and managed together.

Common List Methods

Python provides many built-in methods for working with lists.

Method Description
append() Adds an item at the end
insert() Inserts an item at a specific position
remove() Removes a specified item
pop() Removes an item by index
clear() Removes all items
sort() Sorts the list
reverse() Reverses the list
count() Counts occurrences of an item
index() Returns item position
copy() Creates a copy of the list

Example:


numbers = [5, 2, 8, 1]
numbers.sort()
print(numbers)

Output:

[1, 2, 5, 8]

Common Mistakes

1. Using an Invalid Index

When learning Python lists, beginners often make these mistakes.

Example:


fruits = ["Apple", "Banana"]
print(fruits[5])

Error:


IndexError: list index out of range

Solution:

Always ensure the index exists before accessing it.

2. Confusing append() and extend()

Incorrect:


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

Output:

[1, 2, [3, 4]]

If you want separate elements, use extend():


numbers = [1, 2]
numbers.extend([3, 4])
print(numbers)

Output:

[1, 2, 3, 4]

3. Modifying a List While Iterating

Incorrect:


numbers = [1, 2, 3, 4]

for num in numbers:


numbers.remove(num)

This may skip elements unexpectedly.

Better Approach


numbers = [1, 2, 3, 4]
numbers = [num for num in numbers if num % 2 == 0]
print(numbers)

4. Forgetting Lists are Mutable


colors = ["Red", "Green"]
colors[0] = "Blue"
print(colors)

Output:

[‘Blue’, ‘Green’]

Changes affect the original list.

Conclusion

Python Lists are one of the most important and frequently used data structures in Python. They allow developers to store multiple values, access items using indexes, modify data, and perform various operations efficiently. Because lists are ordered, mutable, and flexible, they are used in almost every Python application.

Python List – Interview Questions

Q 1: What is a list in Python?
Ans: A list is an ordered collection of items that can store multiple data types.
Q 2: How do you create a list?
Ans: Using square brackets, e.g., my_list = [1, 2, 3].
Q 3: Are lists mutable in Python?
Ans: Yes, you can modify, add, or remove elements in a list.
Q 4: Can a list contain duplicate elements?
Ans: Yes, lists can store duplicate values.
Q 5: How do you find the length of a list?
Ans: By using the len() function.

Python List – Objective Questions (MCQs)

Q1. Which of the following correctly creates a list in Python?






Q2. What is the output of the following code?

fruits = ['apple', 'banana', 'cherry']
print(fruits[1]) 






Q3. Which of the following methods is used to add an element at the end of a list?






Q4. What will be the output of the following code?

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






Q5. Which of the following statements is true about lists in Python?






Related Python Tutorials