Sitemap

Lists in Python

7 min readFeb 27, 2026

--

This article provides a complete understanding of Python Lists, covering theory, syntax, examples, internal behavior, and interview questions. If you want to master Python fundamentals, understanding lists deeply is essential.

What is a List in Python?

A List is a built-in data structure in Python used to store multiple items in a single variable.

Lists are:

  • Ordered
  • Mutable (changeable)
  • Allow duplicate values
  • Can store different data types

Syntax:

my_list = [item1, item2, item3]

Example:

numbers = [1, 2, 3, 4]
names = ["Raghav", "Amit", "Sita"]
mixed = [1, "Hello", 3.14, True]

List items are indexed, the first item has index [0], the second item has index [1] etc.

Characteristics of Lists

1. Ordered:

When we say that lists are ordered, it means that the items have a defined order, and that order will not change, but with the help of list methods you can change the order.

If you add new items to a list, the new items will be placed at the end of the list.

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

2. Mutable (Changeable)

The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.

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

3. Allow Duplicates

Since lists are indexed, lists can have items with the same value:

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

4. Heterogeneous Data Types

In Python, a List is one of the most flexible data structures because it can store multiple data types in a single collection. This property is called Heterogeneous Data Type Support.

my_list = [10, 3.14, "Python", True]
print(my_list)

5. Dynamic Size

Dynamic Size — meaning the list can grow or shrink at runtime automatically.

You don’t need to declare the size in advance (unlike arrays in many other languages).

numbers = [10, 20, 30]

print(numbers)

numbers.append(40)
numbers.append(50)

print(numbers)
#output
# [10, 20, 30]
# [10, 20, 30, 40, 50]

Accessing Elements in a List

Each element in a list has an index number.

1. Using Indexing

Indexing starts from 0 (not 1)

numbers = [10, 20, 30, 40]

print(numbers[0]) # First element
print(numbers[2]) # Third element

2. Negative Indexing

Python allows accessing elements from the end using negative numbers.

print(numbers[-1])  # Last element
print(numbers[-2]) # Second last

List Indexing and Splitting (Slicing)

List Indexing and Slicing are used to access elements from a list.

  • Indexing → Access a single element
  • Slicing → Access multiple elements (sub-list)

1. List Indexing

Indexing means retrieving elements using their position (index number)

Key Rules :

  • Index starts from 0
  • Each element has a unique index
  • Works left → right

Example:

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

print(numbers[0]) # 10
print(numbers[2]) # 30
print(numbers[4]) # 50

Negative Indexing

Access elements from the end.

numbers = [10, 20, 30, 40, 50]
print(numbers[-1]) # 50
print(numbers[-2]) # 40

2. List Splitting (Slicing)

Slicing means extracting a portion (sub-list) from the main list.

Syntax :

list[start : end : step]
# Here:
# start -> Starting index (inclusive)
# end -> Ending index (exclusive)
# step -> jump interval

Example:

# 1. Basic
nums = [10, 20, 30, 40, 50]

print(nums[1:4]) # [20, 30, 40]
print(nums[:3]) # [10, 20, 30]
print(nums[2:]) # [30, 40, 50]
print(nums[::2]) # [10, 30, 50]

#2. Reverse List Using Slicing
print(nums[::-1]) # [50, 40, 30, 20, 10]

Changing, Adding, and Removing Elements

Python lists are mutable — meaning you can modify them after creation.

You can:

  • Change elements
  • Add elements
  • Remove elements

1. Change elements

You can modify an element using its index.

Syntax:

list[index] = new_value

Example:

# 1. change the value of a specific item
fruits = ["Apple", "Banana", "Mango"]
fruits[1] = "orange"
print(fruits)
# output:
# ['Apple', 'Orange', 'Mango']
# -----------------------------------------------------------


# 2. Changing Multiple Elements (Slicing)
nums = [1,2,3,4,5]
nums[1:4] = [20,30,40]
print(nums)
# output:
# [1,20,30,40,5]

2. Adding Elements to a List

Python provides multiple methods to add elements.

Get Saurabh Gandhi’s stories in your inbox

Join Medium for free to get updates from this writer.

Examples:

numbers = [1,2,3]

# 1. append() → Add at End
numbers.append(4)
print(numbers)
# output:
# [1, 2, 3, 4]
# -----------------------------------------------------------


# 2. insert() → Add at Specific Position
numbers.insert(1,10)
print(numbers)
# output:
# [1, 10, 2, 3, 4]
# -----------------------------------------------------------


# 3. extend() → Add Multiple Elements
numbers.extend([5,6,7])
print(numbers)
# output:
# [1, 10, 2, 3, 4, 5, 6, 7]
# -----------------------------------------------------------

# 4. Adding Nested List
numbers.append([8,9])
print(numbers)
# output:
# [1, 10, 2, 3, 4, 5, 6, 7, [8, 9]]
# -----------------------------------------------------------

# 5. Using + Operator
list1 = [1,2]
list2 = [3,4]
result = list1 + list2
print(result)
# output:
# [1, 2, 3, 4]

3. Removing Elements from a List

There are several ways to remove elements.


# 1. remove() → Remove by Value
numbers = [10,20,30,20]
numbers.remove(20)
print(numbers)
# Removes first occurrence only.
# output:
# [10,30,20]
# -----------------------------------------------------------


# 2. pop() → Remove by Index
numbers = [10,20,30,20]
numbers.pop(2)
print(numbers)
# output:
# [10,20,20]

# If no index → removes last element.
numbers = [10,20,30,20]
numbers.pop()
print(numbers)
# output:
# [10,30,20]
# -----------------------------------------------------------


# 3. del Statement
numbers = [10,20,30,20]
del numbers[1]
print(numbers)
# output:
# [10,30,20]

# Delete multiple:
numbers = [10,20,30,40]
del numbers[1:3]
print(numbers)
# output:
# [10,40]
# -----------------------------------------------------------


# 4. clear() → Remove All Elements
numbers = [10,20,30,20]
numbers.clear()
print(numbers)
# output:
# []

Organizing a List in Python

Organizing a list means arranging its elements in a structured or meaningful order.

1. Sorting a List →sort()

The sort() method arranges elements in ascending order by default.

# 1. Numbers
numbers = [50,10,40,20,30]
numbers.sort()
print(numbers)
# output:
# [10, 20, 30, 40, 50]
# -----------------------------------------------------------


# 2. Strings
fruits = ["Mango","Apple","Orange","Banana"]
fruits.sort()
print(fruits)
# Alphabetical order is applied.
# output:
# ['Apple', 'Banana', 'Mango', 'Orange']
# -----------------------------------------------------------

2. Descending Order → reverse=True

numbers = [10, 50, 20, 40]
numbers.sort(reverse=True)
print(numbers)
# output:
# [50, 40, 20, 10]
# -----------------------------------------------------------

3. Temporary Sorting → sorted()

sorted() returns a new sorted list without changing the original.

numbers = [40, 10, 30, 20]
new_list = sorted(numbers)
print(new_list)
print(numbers)
# output:
# [10, 20, 30, 40]
# [40, 10, 30, 20]
# -----------------------------------------------------------

4. Reverse a List → reverse()

This does not sort — it only flips order.

numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
# output:
# [4, 3, 2, 1]
# -----------------------------------------------------------

5. Sorting with Key

words = ["Pyhton","AI","Data","ML"]
words.sort(key=len)
print(words)
# output:
# ['AI', 'ML', 'Data', 'Python']
# -----------------------------------------------------------

# Sort by Last Character
words = ["Pyhton","AB","Data","ML","ABC"]
words.sort(key=lambda x: x[-1])
print(words)
# output:
# ['AB', 'ABC', 'ML', 'Data', 'Pyhton']
# -----------------------------------------------------------

6. Case-Insensitive Sorting

names = ["rahul", "Amit", "suresh", "Ankit"]
names.sort(key=str.lower)
print(names)
# output:
# ['Amit', 'Ankit', 'rahul', 'suresh']
# -----------------------------------------------------------

7. Organizing Nested Lists

student = [
["Amit",85],
["Rahul",92],
["Neha",78],
]

student.sort(key=lambda x:x[1])
print(student)
# output:
# [['Neha', 78], ['Amit', 85], ['Rahul', 92]]
# -----------------------------------------------------------

8. Removing Duplicates (Basic Organization)

numbers = [1, 2, 2, 3, 4, 4]
unique = list(set(numbers))
print(unique)
# output:
# [1, 2, 3, 4]
# -----------------------------------------------------------

List Comprehensions

List Comprehension is a concise, readable, and powerful way to create lists in a single line of code.

It replaces traditional loops + append logic.

Syntax :

[expression for item in iterable]

Traditional Loop vs List Comprehension

  • Without List Comprehension
square = []
for i in range(5):
square.append(i**2)
print(square)
# output:
# [0, 1, 4, 9, 16]
# -----------------------------------------------------------
  • With List Comprehension
square = [i**2 for i in range(5)]
print(square)
# output:
# [0, 1, 4, 9, 16]
# -----------------------------------------------------------

1. Using Conditions (Filtering)

[expression for item in iterable if condition]

Examples:

# 1. Even Numbers
evens = [i for i in range(10) if i%2==0]
print(evens)
# output:
# [0, 2, 4, 6, 8]
# -----------------------------------------------------------


# 2. Strings Length Filter
words = ["AI", "Python", "ML", "Data"]
long_words = [w for w in words if len(w)>2]
print(long_words)
# output:
# ['Python', 'Data']
# -----------------------------------------------------------

2. If-Else in List Comprehension

[expression_if_true if condition else expression_if_false for item in iterable]

Example:

labels = ["even" if i%2==0 else "odd" for i in range(5)]
print(labels)
# output:
# ['even', 'odd', 'even', 'odd', 'even']
# -----------------------------------------------------------

3. Nested List Comprehension

Example:

# 1. Matrix Flattening
matrix= [[1,2,3],[4,5,6],[7,8,9]]
flat = [num for row in matrix for num in row]
print(flat)
# output:
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
# -----------------------------------------------------------


# 2. Multiplication Table
table = [i*j for i in range(1,4) for j in range(1,4)]
print(table)
# output:
# [1, 2, 3, 2, 4, 6, 3, 6, 9]
# -----------------------------------------------------------

4. Nested Comprehension (2D List Creation)

matrix = [[i for i in range(3)] for j in range(3)]
print(matrix)
# output:
# [[0,1,2],
# [0,1,2],
# [0,1,2]]
# -----------------------------------------------------------

5. Using Functions Inside Comprehension

def square(x):
return x*x

result = [square(i) for i in range(5)]
print(result)
# output:
# [0, 1, 4, 9, 16]
# -----------------------------------------------------------

6. String Manipulation

names = ["raghav", "amit", "neha"]
capitalized = [name.upper() for name in names]
# output:
# ['RAGHAV', 'AMIT', 'NEHA']
# -----------------------------------------------------------

7. Dictionary to List

data = {"a":1, "b":2, "c":3}
pairs = [(k,v) for k,v in data.items()]
print(pairs)
# output:
# [('a', 1), ('b', 2), ('c', 3)]
# -----------------------------------------------------------

8. Removing Duplicates

nums = [1,2,2,3,4,4]
unique = [x for i,x in enumerate(nums) if x not in nums[:i]]
# output:
# [1, 2, 3, 4]
# -----------------------------------------------------------

List Function

Questions

  1. What is a List in Python?
  2. Can lists store multiple data types?
  3. How to access list elements?
  4. What is negative indexing?
  5. What is slicing?
  6. How to find list length?
  7. How to add elements?
  8. How to remove elements?
  9. Difference between append() and extend()?
  10. Difference between remove() and pop()?
  11. Difference between sort() and sorted()?
  12. How to reverse a list?
  13. What is list mutability?
  14. How to count occurrences?

--

--

Saurabh Gandhi
Saurabh Gandhi

Python • AI • Tech Learning Writing simple guides that make coding easy for everyone.