Python Lists
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Python Lists

Kishore V

Python Lists

A list in Python is a built-in data structure used to store multiple values in a single variable. Lists are flexible, easy to use, and widely applied when working with collections of related data.

Creating a List

Lists are created using square brackets [], with items separated by commas.

['pen', 'notebook', 'eraser']

What Is a List?

In Python, a list is one of the four core collection data types:

  • List – Ordered, changeable, allows duplicate values
  • Tuple – Ordered, unchangeable, allows duplicate values
  • Set – Unordered, unindexed, no duplicate values
  • Dictionary – Ordered, changeable, stores data as key–value pairs

Each collection type is designed for a different use case.

List Characteristics

1. Ordered

Lists maintain a fixed order. Items appear in the same sequence in which they were added. New elements are always added at the end unless specified otherwise.

['red', 'green', 'blue']

2. Changeable (Mutable)

Lists can be modified after creation—you can add, update, or remove elements.

[2, 10, 6]

3. Allows Duplicate Values

Lists can store the same value multiple times because each item has a unique index.

['John', 'Emma', 'John', 'Alex']

Indexing in Lists

Each list item has a position called an index, starting from 0.

Python C++

Finding the Length of a List

Use the len() function to determine how many elements a list contains.

3

List Items and Data Types

A list can store elements of any data type.

['Alex', 25, True, 5.8]

Checking the Data Type of a List

From Python’s perspective, lists are objects of type list.

<class 'list'>

Creating a List Using the list() Constructor

You can also create a list using the list() constructor, especially when converting other iterable data types.

['mango', 'orange', 'grapes']

Note: Double parentheses are used because the inner one defines a tuple.

Python – Accessing List Items

In Python, list items are stored in an indexed sequence, which allows you to access specific elements by referring to their position. Indexing makes it easy to retrieve individual items or slices of data from a list.

Accessing Items by Index

orange

Explanation: Index 1 refers to the second element in the list. Python uses zero-based indexing.

Negative Indexing

Negative indexing allows access to list items from the end. -1 refers to the last item, -2 refers to the second last item, etc.

grapes

Accessing a Range of Items

You can retrieve multiple items at once using slicing.

['eraser', 'scale', 'marker']

Explanation: Starts from index 2 (included) and ends at index 5 (excluded).

Omitting the Start Index

['pen', 'pencil', 'eraser']

Omitting the End Index

['pencil', 'eraser', 'scale', 'marker']

Range of Negative Indexes

['eraser', 'scale', 'marker']

Checking if an Item Exists in a List

Use the in keyword to check whether a specific value exists in a list. Commonly used in validation and conditional logic.

Yes, 'blue' is available in the list

Python – Changing List Items

Python lists are mutable, which means their contents can be modified after creation. You can update individual elements, replace multiple values at once, or insert new items at a specific position.

Changing a Single Item

['mango', 'pineapple', 'grapes']

Changing Multiple Items Using a Range

['mango', 'kiwi', 'strawberry', 'papaya', 'apple']

Replacing with More/Fewer Items Than Removed

If you insert more elements than you replace, the list size increases:

['mango', 'kiwi', 'strawberry', 'grapes']

If you insert fewer elements than you remove, the list size decreases:

['mango', 'banana']

Inserting Items Without Replacing Existing Ones

To add a new item at a specific position without removing any elements, use the insert() method. Existing elements shift to the right.

['mango', 'orange', 'apple', 'grapes']

Python – Adding Items to a List

Adding an Item Using append()

['red', 'blue', 'green', 'yellow']

Inserting an Item at a Specific Position

['red', 'orange', 'blue', 'green']

Extending a List with Another Iterable

The extend() method allows you to add multiple items at once by appending elements from another list, tuple, set, or dictionary.

['pen', 'pencil', 'eraser', 'marker', 'scale', 'sharpener']

Example: Adding a Tuple to a List

['hammer', 'screwdriver', 'wrench', 'pliers']
Method Adds Position
append() One item End of list
insert() One item Specific index
extend() Multiple items End of list

Python – Removing List Items

Removing an Item by Value

The remove() method deletes the first occurrence of a specified value. If the list contains duplicates, it deletes only the first match.

['mango', 'apple', 'banana', 'kiwi']

Removing an Item by Index Using pop()

The pop() method removes an element based on its index. If no index is specified, it removes the last element.

['red', 'green']
['red', 'blue']

Removing an Item Using del

The del keyword can remove an element at a specific index or delete the entire list object.

[20, 30]
# List is deleted entirely from memory

Clearing All Items from a List

[]

Python – Looping Through a List

Looping Through List Items Using a for Loop

mango orange grapes

Looping Using Index Numbers

Index 0: mango Index 1: orange Index 2: grapes

Looping Through a List Using a while Loop

mango orange grapes

Looping with List Comprehension

mango orange grapes

List Comprehension in Python

List comprehension is a concise and readable way to create a new list from an existing iterable. It reduces multiple lines of code into a single, expressive statement.

General Syntax:

Traditional Approach (Without List Comprehension)

['grape', 'melon', 'berry']

Using List Comprehension (Recommended)

['grape', 'melon', 'berry']

Using Conditions as Filters

['blue', 'green', 'yellow']

Without Any Condition

[1, 2, 3, 4, 5]

Working with Different Iterables

[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]

Modifying Values Using Expressions

['ALICE', 'BOB', 'CHARLIE']
['active', 'active', 'active', 'active', 'active']

Conditional Expressions Inside List Comprehension

['apple', 'orange', 'cherry']

Case-Insensitive Sorting in Python Lists

['Apple', 'Cherry', 'grapes', 'mango']

Case-Insensitive Sorting Using a Key Function

['Apple', 'Cherry', 'grapes', 'mango']

Reversing a List

['Cherry', 'mango', 'Apple', 'grapes']

Tip: Sort and reverse at the same time:

['mango', 'grapes', 'Cherry', 'Apple']

Copying a List in Python

In Python, assigning one list to another using = does not create a new list. Both variables point to the same list object.

['pen', 'pencil', 'eraser', 'marker']

1. Using the copy() Method

['pen', 'pencil', 'eraser']

2. Using the list() Constructor

['pen', 'pencil', 'eraser']

3. Using the Slice Operator (:)

['pen', 'pencil', 'eraser']

Joining (Concatenating) Lists in Python

1. Using the + Operator

['x', 'y', 'z', 10, 20, 30]

2. Appending Elements One by One

['x', 'y', 'z', 10, 20, 30]

3. Using the extend() Method

['x', 'y', 'z', 10, 20, 30]
Our website uses cookies to enhance your experience. Learn More
Accept !