Python Lists: Arrays, Methods and Iteration

Learn everything about Python lists. Discover how to create, slice, modify, and iterate through arrays in Python natively.

Try Python Lists Code

Overview

In Python, Lists are one of the four built-in data types used to store collections of data. You can think of a list as a dynamic array that holds a sequence of items in a specific order. Lists are incredibly versatile and are heavily relied upon in almost every Python script written, from simple to-do applications to complex machine learning pipelines.

Unlike arrays in statically typed languages that enforce uniform data types, Python lists can be completely heterogeneous. This means a single list can comfortably hold a mixture of integers, floats, strings, objects, and even other nested lists. You declare a list by enclosing your items in square brackets `[]`, separating each item with a comma.

Lists are ordered and indexed. The items inside a list are mapped to an integer index starting at `0`. So, the first item is accessed via `my_list[0]`, the second via `my_list[1]`, and so on. Python also supports negative indexing, a uniquely powerful feature where `my_list[-1]` retrieves the very last item, `my_list[-2]` the second to last, bypassing the need to calculate the list length manually.

Lists are fully mutable, meaning they can be modified in-place after they are created. You can reassign specific items by index, or utilize built-in methods. Methods like `.append()` add an item to the end, `.insert()` injects an item at a specific index, and `.remove()` or `.pop()` delete items. Lists also feature `.sort()` for algorithmic sorting and `.reverse()` to flip the sequence.

A powerful capability unique to Python lists is "slicing". Slicing allows you to extract a sub-list by specifying a start index, a stop index, and a step size, formatted as `my_list[start:stop:step]`. This allows developers to grab chunks of arrays, copy arrays, or even reverse them in a single line of highly optimized C-backed code, making data manipulation remarkably expressive.

Code Example

Demonstrating initialization, indexing, modification and standard list iteration.

fruits = ["apple", "banana", "cherry"]

fruits.append("orange") # Add item
fruits[1] = "mango"     # Change item
fruits.sort()           # Alphabetical sort

print(f"List: {fruits}")
print(f"First fruit: {fruits[0]}")
print(f"Total fruits: {len(fruits)}")
Terminal Output
List: ['apple', 'cherry', 'mango', 'orange']
First fruit: apple
Total fruits: 4

Real-world Use Cases

  • Holding ordered collections like a queue of tasks
  • Accumulating data points over time
  • Implementing stacks and queues

Frequently Asked Questions

What is a list comprehension?

It is a shorter syntax when you want to create a new list based on the values of an existing list. Example: [x for x in fruits if "a" in x]

What is the difference between list and tuple?

Lists are mutable (changeable) while tuples are immutable (unchangeable). Tuples use parentheses ().

Keep Learning

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.