Skip to main content

Introduction to Python Containers

Explore Python's fundamental data structures: lists, strings, tuples, dictionaries, and sets. Learn about their unique properties, including order, mutability, and how to access and manipulate data within them.

Save this reading to your study set

Sign up free

In Python, a "container" is a compound object designed to hold other objects. Because they can store multiple items at once, they are often referred to as compound objects. These containers are essential for organizing data, allowing you to group related information—such as a list of names, a collection of coordinates, or a dictionary of word translations—into a single variable.

Python provides several distinct types of containers, each with unique rules regarding order, accessibility, and whether the contents can be changed after creation. In this article, we will explore the five primary types: lists, strings, tuples, dictionaries, and sets.

Lists: Ordered and Mutable Sequences

A list is an ordered set of values where each value is identified by a specific position called an index. The individual items within a list are known as its elements. One of the most powerful features of lists is that they can hold mixed types; a single list might contain integers, floats, and strings all at once.

A Python list containing elements of mixed data types: an integer (42), a string ("hello"), a float (3.14), and a boolean (True). Each element is associated with a zero-based index, starting from 0.
A Python list containing elements of mixed data types: an integer (42), a string ("hello"), a float (3.14), and a boolean (True). Each element is associated with a zero-based index, starting from 0.

List Indexing and Slicing

Because lists are ordered, you can access specific elements using square brackets [] and a positional index. It is crucial to remember that Python uses zero-based indexing, meaning the first element is at index 0.

  • Indexing: a[0] retrieves the first element. You can also use negative integers to count backward from the end of the list; for example, a[-1] is the last item.
  • Slicing: You can extract a sub-section of a list using the syntax a[start:stop]. This returns elements from the start index up to, but not including, the stop index.

Keep reading for free.

Create an account to finish this reading and make it part of your study routine.

Continue reading free

Already have an account? Log in

Updated

Study it your way.

Import this reading into your study set to get started with another study mode.

Make it your own.

Keep this reading in a new study set, or add it to one you already use.