Feature image for blog post ‘Python Lists Explained’ with title and subtitle highlighting examples, slicing, and methods in Python.

Python Lists Explained (With Examples, Slicing & Methods)

Estimated reading time: 4.5 mins read

What Is a List in Python?

A list is a collection used to store multiple values in a single variable.

Key Characteristics of Lists

  • Ordered (items have a fixed position)
  • Mutable (can be changed after creation)
  • Allows duplicate values
  • Can store mixed data types

Creating a List in Python

Let’s start with a simple example: a list of student marks.

marks = [94.4, 87.5, 95.2, 66.4, 45.1]

print("Marks:", marks)
print("Type of marks:", type(marks))
print("Total number of marks:", len(marks))

Explanation:

  • The list stores float values
  • type() confirms it is a list
  • len() gives the number of elements

Output:

<class 'list'>
5

Accessing List Elements Using Index

Python lists use zero-based indexing, meaning the first element starts at index 0.

print("First mark:", marks[0])
print("Second mark:", marks[1])

Important Tip:

Trying to access an index that does not exist will cause an IndexError.


Lists Can Store Mixed Data Types

Unlike some languages, Python allows lists to store different data types together.

student = ["Nishi", 96.7, 17, "Delhi"]
print("Student details:", student)

Updating List Values (Mutability)

Lists are mutable, meaning you can change their content.

student[0] = "Nishtha"
print("Updated student name:", student[0])

This is a major advantage of lists over strings.


Another Example: List of Integer Marks

student_marks = [59, 89, 58, 70, 90]
print("Original student marks:", student_marks)

This list stores only integer values and is commonly used in real projects like result processing.


List Slicing in Python

Slicing allows you to extract a portion of a list.

Syntax

list[start : end]   # end index is excluded

Examples from the Code

print("Marks from index 1 to end:", student_marks[1:])
print("Marks from start to index 3:", student_marks[:4])
print("Marks from index -3 to -2:", student_marks[-3:-1])

Explanation:

  • student_marks[1:] → from index 1 till the end
  • student_marks[:4] → from start to index 3
  • Negative indexing starts from the end of the list

Commonly Used List Methods in Python

Python provides powerful built-in methods to work with lists efficiently.


append() – Add Element at the End

student_marks.append(95)

Adds a new value at the end of the list.


sort() – Sort the List

student_marks.sort()

Sorts the list in ascending order.

student_marks.sort(reverse=True)

Sorts the list in descending order.


reverse() – Reverse the List Order

student_marks.reverse()

Reverses the current order of elements.


insert(index, value) – Insert at Specific Position

student_marks.insert(2, 43)

Inserts 43 at index 2 without removing existing elements.


pop(index) – Remove Element by Index

student_marks.pop(2)

Removes and returns the element at index 2.


Difference Between Strings and Lists in Python

This is a very common interview and beginner confusion, so let’s clear it properly.

FeatureStringList
Data TypeText (characters)Collection of values
Mutable❌ No✅ Yes
Indexing✅ Yes✅ Yes
Slicing✅ Yes✅ Yes
Modification❌ Not allowed✅ Allowed
Example"Python"["P", "y", "t", "h", "o", "n"]

Example Difference

# String (immutable)
name = "Python"
# name[0] = "J"  ❌ Error

# List (mutable)
letters = ["P", "y", "t", "h", "o", "n"]
letters[0] = "J"  # ✅ Allowed

Key takeaway:

  • Use strings for text that should not change.
  • Use lists when data needs to be updated or manipulated.

When Should You Use Lists?

  • Storing marks, prices, scores
  • Managing user data
  • Handling collections of items
  • Working with dynamic data

In real-world Python applications, lists are used everywhere from web apps to data science.


Important Interview Qs&As

Q1.What is a list in Python?

A1. A list in Python is a built-in data structure used to store multiple values in a single variable. Lists are ordered, mutable, and allow duplicate values. They can store elements of different data types.

Q2. How is a list different from an array?

A2.

  • Lists can store mixed data types (int, float, string, etc.).
  • Arrays (from the array module) store only one data type.
  • Lists are more flexible and commonly used in Python.
Q3. Are Python lists mutable?

A3. Yes, lists are mutable. This means you can change, add, or remove elements after the list is created.

marks = [10, 20, 30]
marks[0] = 50


Q4. What is the difference between append() and insert()?

  • append(): Adds an element at the end of the list
  • insert(): Adds an element at a specific index
Q5. What does the pop() method do?

A5. The pop() method removes and returns an element from the list using its index.
If no index is provided, it removes the last element.

Q6. What is the difference between sort() and reverse()?

A6.

  • sort() arranges elements in ascending or descending order.
  • reverse() only reverses the current order of elements.
Q7. What is the difference between a string and a list?

A7.

  • Strings are immutable, lists are mutable.
  • Strings store characters, lists store elements.
  • You cannot modify individual characters in a string.
Q8. What is negative indexing in lists?

A8.

  • Negative indexing allows accessing elements from the end of the list.
  • -1 refers to the last element, -2 to the second last, and so on.
Q9. Can lists store multiple data types?

A9. Yes, Python lists can store different data types in the same list.

Q10. Can a list contain duplicate values?

A10. Yes, lists allow duplicate values.

Happy Learning !


Comments

Leave a Reply