codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

19 / 68 topics
19Python Lists20Python Tuples21Python Dictionaries22Python Sets23Python Arrays
Tutorials/Python Programming/Python Lists
🐍Python Programming

Python Lists

Updated 2026-05-15
30 min read

Python Lists

Lists are one of the most fundamental data structures in Python, allowing you to store multiple items in a single variable. They are versatile, mutable, and can hold elements of different data types. Understanding how to work with lists is crucial for any Python programmer, as they form the backbone of many applications.

In this tutorial, we'll cover how to create lists, access their elements using indexing and slicing, and modify them using various built-in methods. By the end of this guide, you'll be comfortable working with lists in your Python programs.

Creating Lists

A list in Python is an ordered collection of items that can be of different types. Lists are defined by enclosing elements in square brackets [], separated by commas.

create_list.py
1# Creating a simple list
2fruits = ['apple', 'banana', 'cherry']
3print(fruits)
Output
['apple', 'banana', 'cherry']

Lists can contain elements of different types, including integers, strings, and even other lists.

mixed_list.py
1# Creating a mixed-type list
2mixed = [1, 'hello', 3.14, ['nested', 'list']]
3print(mixed)
Output
[1, 'hello', 3.14, ['nested', 'list']]

Indexing

You can access individual elements of a list using their index. Lists are zero-indexed, meaning the first element is at index 0, the second element is at index 1, and so on.

index_access.py
1# Accessing elements by index
2fruits = ['apple', 'banana', 'cherry']
3print(fruits[0]) # Output: apple
4print(fruits[2]) # Output: cherry
Output
apple
cherry

You can also use negative indices to access elements from the end of the list. -1 refers to the last element, -2 to the second-to-last, and so on.

negative_index.py
1# Accessing elements with negative index
2fruits = ['apple', 'banana', 'cherry']
3print(fruits[-1]) # Output: cherry
4print(fruits[-2]) # Output: banana
Output
cherry
banana

Slicing

Slicing allows you to extract a portion of a list. The syntax for slicing is list[start:stop:step], where start is the starting index, stop is the ending index (exclusive), and step is the interval between elements.

slicing.py
1# Slicing a list
2fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
3print(fruits[1:4]) # Output: ['banana', 'cherry', 'date']
4print(fruits[:3]) # Output: ['apple', 'banana', 'cherry']
5print(fruits[2:]) # Output: ['cherry', 'date', 'elderberry']
Output
['banana', 'cherry', 'date']
['apple', 'banana', 'cherry']
['cherry', 'date', 'elderberry']

You can also use negative indices and a step value to slice lists in reverse.

reverse_slicing.py
1# Reverse slicing
2fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
3print(fruits[-3:-1]) # Output: ['cherry', 'date']
4print(fruits[::-1]) # Output: ['elderberry', 'date', 'cherry', 'banana', 'apple']
Output
['cherry', 'date']
['elderberry', 'date', 'cherry', 'banana', 'apple']

Mutability

One of the key features of lists is their mutability, meaning you can change their content without changing their identity. You can modify elements by assigning new values to specific indices.

mutability.py
1# Modifying list elements
2fruits = ['apple', 'banana', 'cherry']
3fruits[1] = 'blueberry'
4print(fruits) # Output: ['apple', 'blueberry', 'cherry']
Output
['apple', 'blueberry', 'cherry']

You can also add or remove elements from a list using various methods.

List Methods

Python provides several built-in methods to manipulate lists. Let's explore some of the most commonly used ones.

append()

The append() method adds an element to the end of the list.

append.py
1# Appending elements
2fruits = ['apple', 'banana']
3fruits.append('cherry')
4print(fruits) # Output: ['apple', 'banana', 'cherry']
Output
['apple', 'banana', 'cherry']

extend()

The extend() method adds all elements from another iterable (like a list or tuple) to the end of the current list.

extend.py
1# Extending a list
2fruits = ['apple', 'banana']
3more_fruits = ['cherry', 'date']
4fruits.extend(more_fruits)
5print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
Output
['apple', 'banana', 'cherry', 'date']

insert()

The insert() method inserts an element at a specified position in the list.

insert.py
1# Inserting elements
2fruits = ['apple', 'banana', 'cherry']
3fruits.insert(1, 'blueberry')
4print(fruits) # Output: ['apple', 'blueberry', 'banana', 'cherry']
Output
['apple', 'blueberry', 'banana', 'cherry']

remove()

The remove() method removes the first occurrence of a specified value from the list.

remove.py
1# Removing elements
2fruits = ['apple', 'banana', 'cherry']
3fruits.remove('banana')
4print(fruits) # Output: ['apple', 'cherry']
Output
['apple', 'cherry']

pop()

The pop() method removes and returns the element at a specified index. If no index is specified, it removes and returns the last item.

pop.py
1# Popping elements
2fruits = ['apple', 'banana', 'cherry']
3removed_fruit = fruits.pop(1)
4print(fruits) # Output: ['apple', 'cherry']
5print(removed_fruit) # Output: banana
Output
['apple', 'cherry']
banana

sort()

The sort() method sorts the list in place.

sort.py
1# Sorting a list
2numbers = [3, 1, 4, 1, 5, 9]
3numbers.sort()
4print(numbers) # Output: [1, 1, 3, 4, 5, 9]
Output
[1, 1, 3, 4, 5, 9]

reverse()

The reverse() method reverses the order of elements in the list.

reverse.py
1# Reversing a list
2numbers = [1, 2, 3, 4, 5]
3numbers.reverse()
4print(numbers) # Output: [5, 4, 3, 2, 1]
Output
[5, 4, 3, 2, 1]

copy()

The copy() method creates a shallow copy of the list.

copy.py
1# Copying a list
2original = [1, 2, 3]
3copied = original.copy()
4print(copied) # Output: [1, 2, 3]
Output
[1, 2, 3]

Practical Example

Let's create a simple program that manages a to-do list. Users can add tasks, remove completed tasks, and view the current list.

todo_list.py
1# To-Do List Manager
2tasks = []
3
4while True:
5 print("
6To-Do List Manager")
7 print("1. Add Task")
8 print("2. Remove Task")
9 print("3. View Tasks")
10 print("4. Exit")
11
12 choice = input("Enter your choice: ")
13
14 if choice == '1':
15 task = input("Enter the task to add: ")
16 tasks.append(task)
17 print(f"Task '{task}' added.")
18 elif choice == '2':
19 if tasks:
20 task = input("Enter the task to remove: ")
21 if task in tasks:
22 tasks.remove(task)
23 print(f"Task '{task}' removed.")
24 else:
25 print("Task not found.")
26 else:
27 print("No tasks available.")
28 elif choice == '3':
29 if tasks:
30 print("
31Current Tasks:")
32 for i, task in enumerate(tasks, 1):
33 print(f"{i}. {task}")
34 else:
35 print("No tasks available.")
36 elif choice == '4':
37 print("Exiting the To-Do List Manager.")
38 break
39 else:
40 print("Invalid choice. Please try again.")

Summary

ConceptDescription
Creating ListsUse square brackets [] to define a list of elements.
IndexingAccess elements by their index, using zero-based indexing.
SlicingExtract portions of a list using the slicing syntax [start:stop:step].
MutabilityLists are mutable; you can modify them without changing their identity.
append()Add an element to the end of the list.
extend()Add all elements from another iterable to the end of the list.
insert()Insert an element at a specified position in the list.
remove()Remove the first occurrence of a specified value from the list.
pop()Remove and return the element at a specified index (or the last if none given).
sort()Sort the list in place.
reverse()Reverse the order of elements in the list.
copy()Create a shallow copy of the list.

What's Next?

Now that you've mastered Python lists, it's time to explore another built-in data structure: tuples. Tuples are similar to lists but immutable, making them suitable for fixed collections of items. In the next tutorial, we'll dive into tuples and their various use cases.

Stay tuned!


PreviousPython break, continue, and passNext Python Tuples

Recommended Gear

Python break, continue, and passPython Tuples