Skip to main content

Command Palette

Search for a command to run...

πŸŽ“Mastering Python Lists – From Basics to Advanced

Updated
β€’17 min readβ€’View as Markdown
πŸŽ“Mastering Python Lists – From Basics to Advanced
V
Learning in public and sharing everything I discover along the way. Follow my journey through SQL, Python, Git, Linux, and software development with beginner-friendly notes, examples, and hands-on projects.

πŸ“š What is a List in Python?

πŸ“– Definition

A List in Python is a container that stores multiple values in one place.

Think of it like a school bag.

πŸŽ’ A school bag can hold many things:

  • πŸ“š Books

  • ✏️ Pencil

  • πŸ“’ Notebook

  • 🍎 Lunch Box

Instead of carrying each item separately, you keep everything inside one bag.

A Python List works the same way. Instead of storing one value at a time, it stores many values together.


πŸ“– Another Easy Definition

A List is a collection of items stored in a single variable.

Or even simpler:

A List is like a box that can hold many values together.


🌍 Real-Life Example

Imagine you have three favorite fruits.

❌ Without a List

fruit1 = "Apple"
fruit2 = "Mango"
fruit3 = "Banana"

Here, we created three different variables.

βœ… Using a List

fruits = ["Apple", "Mango", "Banana"]

Now all the fruits are stored in one variable called fruits.


πŸ“¦ Visual Representation

fruits
   β”‚
   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Apple   β”‚  Mango   β”‚ Banana   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

One list contains many items.


❓ Why Do We Use Lists?

We use lists because they help us:

  • Store multiple values in one variable.

  • Keep related data together.

  • Add or remove items easily.

  • Change items whenever needed.

  • Avoid creating many separate variables.

  • Make programs shorter and easier to manage.


πŸ’» Example 1

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

print(numbers)

Output

[10, 20, 30, 40, 50]

πŸ’» Example 2

students = ["Rahul", "Priya", "Aman", "Riya"]

print(students)

Output

['Rahul', 'Priya', 'Aman', 'Riya']

🍬 Child-Friendly Example

Imagine you have a candy box.

🍫 🍬 🍭 🍩

Instead of holding each candy separately, you keep them all inside one box.

In Python:

candies = ["Chocolate", "Toffee", "Lollipop", "Gum"]

Here:

  • πŸ“¦ Box β†’ List

  • 🍬 Candies β†’ List Items


πŸ“ Remember These Points

  • A List stores multiple items.

  • Items are written inside square brackets [ ].

  • Items are separated by commas ,.

  • A list can store numbers, strings, or different types of data together.

Example:

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

🎯 Key Points

βœ… Stores multiple values in one variable.

βœ… Uses square brackets [ ].

βœ… Items are separated by commas.

βœ… Lists are ordered.

βœ… Lists can be changed after creation (mutable).


πŸ“š Summary

A List in Python is a collection of multiple items stored in a single variable. It helps us keep related data together, making our programs cleaner, shorter, and easier to manage. Lists are written using square brackets [ ], and each item is separated by a comma.


πŸ“Œ Best Definition for Notes

A List in Python is a collection of multiple items stored in a single variable. The items are written inside square brackets [ ] and separated by commas.

🌟 Super Simple Definition

A List is like a box or bag that stores many values together in one place. πŸŽ’

Why do we need Lists?

Without a list

fruit1 = "Apple"
fruit2 = "Mango"
fruit3 = "Banana"
fruit4 = "Orange"

Too many variables!

With a list

fruits = ["Apple", "Mango", "Banana", "Orange"]

Only one variable.

Much easier.


2️⃣ List vs Array

Many beginners think List and Array are the same.

They are similar, but not exactly the same.


Difference 1

Array β†’ Homogeneous

Homogeneous means

All elements must have the same data type.

Example

[10,20,30,40]

βœ” Integers only

or

[1.5,2.8,3.9]

βœ” Floats only

You cannot mix different data types in a traditional array.


List β†’ Heterogeneous

Heterogeneous means

Different data types can be stored together.

Example

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

Here we have

  • String

  • Integer

  • Float

  • Boolean

All inside one list.

Python allows this.


Difference 2

Arrays consume less memory

Since every value is the same type,

the computer knows exactly how much memory each item needs.

Example

10
20
30
40

Every number has the same size.

Memory becomes organized.


Lists consume more memory

Lists can contain

10
"Python"
True
5.6

Each value has a different size.

Python stores extra information about every object.

So Lists use more memory.


Difference 3

Arrays are Faster

Because

  • same data type

  • fixed memory

  • simple calculations

the computer processes arrays very quickly.

Arrays are mostly used in

  • Mathematics

  • Data Science

  • Machine Learning

  • Image Processing


Lists are Slower

Since Python must check

  • What type is this?

  • Integer?

  • String?

  • Float?

for every element,

Lists are a little slower.


Difference 4

Arrays

Mainly used for

βœ” Numerical calculations

Example

Marks
Heights
Weights
Temperatures

Lists

Used for almost everything in Python.

Example

Students
Books
Cities
Shopping Items
Employees

Quick Comparison

Feature List Array
Data Types Different Same
Memory More Less
Speed Slower Faster
Flexibility High Low
Used In General Programming Mathematical Calculations

Easy Way to Remember

Think about a classroom.

🧺 List

A toy box

Car
Ball
Doll
Book
Pencil

Different items together.

This is a List.


πŸ“¦ Array

A box of apples

🍎
🍎
🍎
🍎
🍎

Only apples.

Everything is the same.

This is an Array.


Interview Definition

List

A List is an ordered, mutable collection in Python that can store multiple values of different data types inside a single variable.


Array

An Array is a collection of elements of the same data type stored in contiguous memory locations, making it faster and more memory-efficient for numerical operations.


Key Points to Remember

βœ… List stores multiple values.

βœ… Lists are ordered.

βœ… Lists are mutable (can be changed).

βœ… Lists allow duplicate values.

βœ… Lists can store different data types.

βœ… Arrays store only one data type.

βœ… Arrays are faster than lists.

βœ… Arrays use less memory than lists.

1️⃣ Create a List

Definition

Creating a list means making a new list and storing values inside it.

In Python, a list is created using square brackets [ ].

Syntax

list_name = [item1, item2, item3]

Example 1

fruits = ["Apple", "Banana", "Mango"]

Output

['Apple', 'Banana', 'Mango']

Example 2

numbers = [10, 20, 30, 40]

Example 3

mixed = ["Vishal", 20, 85.5, True]

A list can store different data types.


Empty List

Sometimes we want to create a list first and add values later.

students = []

Output

[]

Nested List

A list can even contain another list.

data = [
    [1, 2, 3],
    [4, 5, 6]
]

Remember

βœ… Use square brackets []

βœ… Separate values using commas ,


πŸ“š Mastering 2D, 3D, and 4D Lists in Python

A beginner-friendly guide with crystal-clear visualizations


🧩 What is a Dimension?

In Python, dimension refers to the number of levels of nesting in a list.
Think of it like boxes inside boxes:

  • 1D β†’ a single row of items.

  • 2D β†’ a table (rows + columns).

  • 3D β†’ a stack of tables (layers + rows + columns).

  • 4D β†’ a collection of stacks (blocks + layers + rows + columns).

Each extra [] in the index adds one more level.


🟒 1D List – The Foundation

A 1D list is a simple, flat list of elements.

numbers = [10, 20, 30, 40]

πŸͺ‘ Real‑life analogy

A row of chairs, each holding one student.

πŸ‘οΈ Visualization

numbers
β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
β”‚ 10 β”‚ 20 β”‚ 30 β”‚ 40 β”‚
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜
  ↑    ↑    ↑    ↑
  0    1    2    3   ← index

πŸ” Access

print(numbers[2])   # 30

🟑 2D List – The Grid

A 2D list is a list of lists – it forms a table with rows and columns.

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

πŸͺ‘ Real‑life analogy

A classroom seating chart – each seat is identified by row and column.

πŸ‘οΈ Visualization

         column β†’
         0    1    2
      β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
row 0 β”‚  1 β”‚  2 β”‚  3 β”‚
      β”œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€
row 1 β”‚  4 β”‚  5 β”‚  6 β”‚
      β”œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€
row 2 β”‚  7 β”‚  8 β”‚  9 β”‚
      β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜

πŸ” Access

  • Entire row: matrix[1] β†’ [4, 5, 6]

  • Single cell: matrix[1][2] β†’ 6
    (row 1, column 2)

πŸ“ Formula

list[row][column]

πŸ”΅ 3D List – The Stack of Tables

A 3D list is a list of 2D lists – it adds a layer (or depth) dimension.

building = [
    [   # Layer 0
        [101, 102],
        [103, 104]
    ],
    [   # Layer 1
        [201, 202],
        [203, 204]
    ]
]

🏒 Real‑life analogy

A building with multiple floors – each floor is a grid of rooms.

πŸ‘οΈ Visualization

Layer 0          Layer 1
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 101 102 β”‚      β”‚ 201 202 β”‚
β”‚ 103 104 β”‚      β”‚ 203 204 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   ↑                  ↑
 floor 0           floor 1

πŸ” Access

  • Entire layer: building[0] β†’ [[101,102],[103,104]]

  • Single room: building[1][0][1] β†’ 202
    (layer 1, row 0, column 1)

πŸ“ Formula

list[layer][row][column]

πŸ”΄ 4D List – The City of Buildings

A 4D list is a list of 3D lists – it adds a block (or building) dimension.

city = [
    [   # Block 0 (Building A)
        [   # Layer 0 (Floor 0)
            [101, 102],
            [103, 104]
        ],
        [   # Layer 1 (Floor 1)
            [201, 202],
            [203, 204]
        ]
    ],
    [   # Block 1 (Building B)
        [   # Layer 0
            [301, 302],
            [303, 304]
        ],
        [   # Layer 1
            [401, 402],
            [403, 404]
        ]
    ]
]

πŸ™οΈ Real‑life analogy

A city with multiple buildings – each building has floors, each floor has rows of rooms.

πŸ‘οΈ Visualization

City
β”œβ”€β”€ Building 0
β”‚   β”œβ”€β”€ Floor 0 : [101 102]
β”‚   β”‚             [103 104]
β”‚   └── Floor 1 : [201 202]
β”‚                 [203 204]
└── Building 1
    β”œβ”€β”€ Floor 0 : [301 302]
    β”‚             [303 304]
    └── Floor 1 : [401 402]
                  [403 404]

πŸ” Access

  • Entire building: city[0] β†’ (the whole 3D list of building 0)

  • Single room: city[1][0][1][0] β†’ 303
    (block 1, layer 0, row 1, column 0)

πŸ“ Formula

list[block][layer][row][column]

🧠 How to Think About Dimensions – The "Box" Analogy

Each dimension is like opening an extra box:

Dimension Indexes Mental Model
1D [i] Box β†’ Item
2D [i][j] Big box β†’ small box β†’ item
3D [i][j][k] Big box β†’ medium box β†’ small box β†’ item
4D [i][j][k][l] Huge box β†’ big box β†’ medium box β†’ small box β†’ item

πŸ“Š Quick Reference Table

Dimension Structure Access Formula Common Use
1D [1, 2, 3] list[idx] Simple sequences
2D [[1,2],[3,4]] list[row][col] Tables, grids, matrices
3D [[[1,2],[3,4]], ...] list[layer][row][col] 3D games, image processing (RGB), volume data
4D [[[[...]]]] list[block][layer][row][col] Time‑series of 3D data, AI (batches of images), simulations


❓ Should Beginners Learn 3D/4D Lists?

Dimension Importance for Beginners
1D ⭐⭐⭐⭐⭐ Essential – used everywhere.
2D ⭐⭐⭐⭐ Very common – games, spreadsheets, matrices.
3D ⭐⭐⭐ Useful – but often covered later (image processing, basic 3D games).
4D ⭐ Not needed initially – more advanced (AI, scientific computing).

Our advice:

  • Master 1D and 2D first – they are the building blocks.

  • Understand the logic of 3D (layers), but you don’t have to memorise complex nesting.

  • When you need 4D, you’ll likely use specialised libraries anyway.


🏁 Conclusion

  • Dimensions are just levels of nesting.

  • The number of brackets in the index = the dimension.

  • Visualise them as rows/tables/stacks/blocks to make sense of them.

  • Start simple, and only go deeper when needed.

2️⃣ Access List Elements

Definition

Access means getting a value from the list.

Python uses index numbers.

The first item always starts from 0.


Example

fruits = ["Apple", "Banana", "Mango", "Orange"]

Index

        0         1         2         3

fruits=["Apple","Banana","Mango","Orange"]

First Item

print(fruits[0])

Output

Apple

Second Item

print(fruits[1])

Output

Banana

Last Item (Negative Index)

Python also supports negative indexing.

        -4       -3       -2       -1

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

Output

Orange

Access Multiple Values (Slicing)

print(fruits[1:3])

Output

['Banana', 'Mango']

Explanation

Start β†’ Index 1

Stop β†’ Index 3 (Not Included)


Remember

βœ… Positive Index β†’ Left to Right

βœ… Negative Index β†’ Right to Left


3️⃣ Edit (Modify) a List

Definition

Edit means changing an existing value.

Lists are Mutable, so we can modify them.


Example

fruits = ["Apple", "Banana", "Mango"]

fruits[1] = "Orange"

print(fruits)

Output

['Apple', 'Orange', 'Mango']

Edit Last Item

fruits[-1] = "Kiwi"

Output

['Apple', 'Orange', 'Kiwi']

Remember

Lists are mutable.

That means they can be changed after creation.


4️⃣ Add Items

Definition

Add means putting new items into the list.

Python provides different methods.


1. append()

Adds one item at the end.

numbers = [10, 20, 30]

numbers.append(40)

print(numbers)

Output

[10, 20, 30, 40]

2. insert()

Adds an item at a specific index.

numbers = [10, 20, 30]

numbers.insert(1, 15)

print(numbers)

Output

[10, 15, 20, 30]

3. extend()

Adds multiple items.

numbers = [10, 20]

numbers.extend([30, 40, 50])

print(numbers)

Output

[10, 20, 30, 40, 50]

Quick Comparison

Method Purpose
append() Add one item at the end
insert() Add one item at a specific position
extend() Add multiple items

5️⃣ Delete Items

Definition

Delete means removing items from the list.


1. remove()

Removes by value.

fruits = ["Apple", "Banana", "Mango"]

fruits.remove("Banana")

print(fruits)

Output

['Apple', 'Mango']

2. pop()

Removes by index.

numbers = [10, 20, 30]

numbers.pop(1)

print(numbers)

Output

[10, 30]

Without an index

numbers.pop()

Removes the last item.


3. del

Deletes an item or the whole list.

numbers = [10, 20, 30]

del numbers[0]

print(numbers)

Output

[20, 30]

Delete the entire list

del numbers

4. clear()

Removes all items but keeps the list.

numbers = [10, 20, 30]

numbers.clear()

print(numbers)

Output

[]

Quick Comparison

Method Removes
remove() Value
pop() Index (or last item)
del Item or whole list
clear() All items

7️⃣ List Operations

Operations are actions we perform on lists.


1. Concatenation (+)

Joins two lists.

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

print(a + b)

Output

[1, 2, 3, 4]

2. Repetition (*)

Repeats a list.

print([1, 2] * 3)

Output

[1, 2, 1, 2, 1, 2]

3. Membership

Checks whether an item exists.

fruits = ["Apple", "Banana"]

print("Apple" in fruits)

Output

True

print("Orange" not in fruits)

Output

True

4. Length

numbers = [10, 20, 30]

print(len(numbers))

Output

3

5. Iteration

for fruit in fruits:
    print(fruit)

Output

Apple
Banana

7️⃣ Common List Functions & Methods

These are the functions you'll use most often.

Function / Method Purpose
len() Returns the number of items
max() Returns the largest value
min() Returns the smallest value
sum() Returns the total of all numbers
sorted() Returns a new sorted list
append() Adds one item
insert() Adds at a specific position
extend() Adds multiple items
remove() Removes by value
pop() Removes by index
clear() Removes all items
index() Finds the index of a value
count() Counts how many times a value appears
sort() Sorts the original list
reverse() Reverses the original list
copy() Creates a copy of the list

Example

numbers = [30, 10, 20, 10]

print(len(numbers))
print(max(numbers))
print(min(numbers))
print(sum(numbers))
print(numbers.count(10))
print(numbers.index(20))

Output

4
30
10
70
2
2

🎯 Chapter Summary

  • βœ… Create β†’ Make a new list using [].

  • βœ… Access β†’ Use indexes (0, 1, -1) to get items.

  • βœ… Edit β†’ Change items because lists are mutable.

  • βœ… Add β†’ Use append(), insert(), and extend().

  • βœ… Delete β†’ Use remove(), pop(), del, and clear().

  • βœ… Operations β†’ Join (+), repeat (*), check (in), measure (len()), and loop through lists.

  • βœ… Functions & Methods β†’ Learn common tools like len(), sum(), sort(), reverse(), count(), and copy().

PYTHON MASTER - SERIES FROM BASICS TO ADVANCE

Part 2 of 3

Here is a professional series description you can use across your GitHub, YouTube, LinkedIn, blog, or social media. **Title** 🐍 PYTHON MASTER – Series From Basics to Advance **Description** Master Python from **absolute beginner** to **advanced level** with this complete learning series. πŸš€ In this series, you'll learn Python step by step with simple explanations, real-world examples, coding exercises, interview questions, and mini projects. Every topic is designed to build a strong programming foundation and prepare you for Data Analytics, Data Science, AI/ML, Automation, and Software Development. πŸ“š What You'll Learn * βœ… Python Basics * βœ… Variables & Data Types * βœ… Input & Output * βœ… Operators * βœ… Conditional Statements * βœ… Loops * βœ… Strings * βœ… Lists * βœ… Tuples * βœ… Sets * βœ… Dictionaries * βœ… Functions * βœ… Modules & Packages * βœ… File Handling * βœ… Exception Handling * βœ… Object-Oriented Programming (OOP) * βœ… Iterators & Generators * βœ… Lambda Functions * βœ… Decorators * βœ… Regular Expressions (Regex) * βœ… NumPy * βœ… Pandas * βœ… Data Visualization * βœ… APIs * βœ… Automation * βœ… Mini & Real-World Projects * βœ… Interview Questions * βœ… Best Coding Practices 🎯 Who Is This Series For? * Beginners with no programming experience * College students * Aspiring Data Analysts & Data Scientists * Python Developers * Anyone preparing for coding interviews πŸš€ Goal By the end of this series, you'll have the skills and confidence to write Python programs, solve real-world problems, build projects, and move on to advanced fields like **Data Science, Machine Learning, AI, Web Development, and Automation**. **Learn β€’ Practice β€’ Build β€’ Master Python** πŸπŸ’»

Up next

Python Sets - Complete Guide (Beginner to Intermediate)

πŸ“š A Comprehensive Resource for Learning Python SetsPerfect for beginners, intermediate learners, and interview preparation Table of Contents Prerequisites What is a Set? Creating Sets Set Prope