Last modified: Feb 11, 2026 By Alexander Williams

Python List of Objects: Create, Manage, and Use

Python lists are powerful. They can hold any data type.

This includes your own custom objects. A list of objects is a common and useful structure.

It lets you manage collections of complex data. You can model real-world groups easily.

Think of a list of students, products, or game characters. This guide will show you how.

We will cover creation, access, modification, and useful operations.

What is a Python Object?

In Python, everything is an object. An object is an instance of a class.

A class is a blueprint. It defines attributes and behaviors.

For a deep dive into classes and instances, see our Python Objects: Classes, Instances, and Methods Guide.

Let's define a simple class to use in our examples.


# Define a simple Book class
class Book:
    def __init__(self, title, author, pages):
        # These are the object's attributes
        self.title = title
        self.author = author
        self.pages = pages

    def __str__(self):
        # Defines how the object is printed
        return f"'{self.title}' by {self.author}"
    

Creating a List of Objects

Creating the list is simple. You instantiate objects and add them to a list.

Use square brackets [] or the list() constructor.


# Create individual Book objects
book1 = Book("The Hobbit", "J.R.R. Tolkien", 310)
book2 = Book("1984", "George Orwell", 328)
book3 = Book("Pride and Prejudice", "Jane Austen", 432)

# Create a list containing these objects
library = [book1, book2, book3]
print(library)
    

[<__main__.Book object at 0x...>, <__main__.Book object at 0x...>, <__main__.Book object at 0x...>]
    

The print output shows memory addresses. This is the default behavior.

Our __str__ method makes it nicer when printing individual items.

Accessing and Modifying Objects in a List

You access objects using their index, just like any list.


# Access the first book in the list
first_book = library[0]
print(f"First book: {first_book}")
print(f"Author: {first_book.author}")

# Modify an object's attribute
library[1].pages = 350
print(f"Updated page count for 1984: {library[1].pages}")
    

First book: 'The Hobbit' by J.R.R. Tolkien
Author: J.R.R. Tolkien
Updated page count for 1984: 350
    

You can loop through the list to work with all objects.


# Loop through and print all books
print("My Library:")
for book in library:
    print(f" - {book}")
    

My Library:
 - 'The Hobbit' by J.R.R. Tolkien
 - '1984' by George Orwell
 - 'Pride and Prejudice' by Jane Austen
    

Adding and Removing Objects

Use list methods like append(), insert(), and remove().


# Add a new book to the end
new_book = Book("Dune", "Frank Herbert", 412)
library.append(new_book)
print(f"Added: {library[-1]}")

# Remove a specific book object
library.remove(book2) # Removes the '1984' book object
print(f"Number of books after removal: {len(library)}")
    

Added: 'Dune' by Frank Herbert
Number of books after removal: 3
    

Sorting a List of Objects

Sorting is a common task. Use the sorted() function or list.sort() method.

You need to specify a key to sort by an object's attribute.


# Sort the library list by the number of pages (ascending)
library_sorted_by_pages = sorted(library, key=lambda book: book.pages)
print("Sorted by pages:")
for book in library_sorted_by_pages:
    print(f" - {book.title}: {book.pages} pages")

# Sort in-place by author's name
library.sort(key=lambda book: book.author)
print("\nSorted by author:")
for book in library:
    print(f" - {book.author}: {book.title}")
    

Sorted by pages:
 - The Hobbit: 310 pages
 - Pride and Prejudice: 432 pages
 - Dune: 412 pages

Sorted by author:
 - Frank Herbert: Dune
 - J.R.R. Tolkien: The Hobbit
 - Jane Austen: Pride and Prejudice
    

Filtering and Searching

List comprehensions are perfect for filtering.

You can create a new list based on a condition.


# Find all books with more than 400 pages
long_books = [book for book in library if book.pages > 400]
print("Books with >400 pages:")
for book in long_books:
    print(f" - {book.title}")

# Find a specific book by title
search_title = "Dune"
found_book = next((book for book in library if book.title == search_title), None)
if found_book:
    print(f"\nFound: {found_book}")
    

Books with >400 pages:
 - Pride and Prejudice
 - Dune

Found: 'Dune' by Frank Herbert
    

Lists of Objects Within Objects

Objects can have attributes that are themselves lists of other objects.

This creates nested, hierarchical data structures.

To explore this concept further, read our guide on Python Objects in Objects: Nested Data Guide.


class Bookshelf:
    def __init__(self, location):
        self.location = location
        self.books = [] # This is a list of Book objects

    def add_book(self, book):
        self.books.append(book)

    def list_books(self):
        print(f"Books on the {self.location} shelf:")
        for book in self.books:
            print(f"  - {book}")

# Create a bookshelf and add books
living_room_shelf = Bookshelf("Living Room")
living_room_shelf.add_book(book1)
living_room_shelf.add_book(new_book)
living_room_shelf.list_books()
    

Books on the Living Room shelf:
  - 'The Hobbit' by J.R.R. Tolkien
  - 'Dune' by Frank Herbert
    

Converting to and From JSON

Often, you need to save or transmit your list of objects.

JSON is a common format. You need to serialize your objects.

Check out our JSON Serialization Guide for Custom Python Objects for detailed methods.

Here's a basic example using a dictionary representation.


import json

# Convert list of objects to list of dictionaries
books_dict_list = [{"title": b.title, "author": b.author, "pages": b.pages} for b in library]

# Convert to JSON string
json_string = json.dumps(books_dict_list, indent=2)
print("JSON Representation:")
print(json_string)

# Convert back from JSON (results in list of dictionaries)
# To convert back to Book objects, you would need additional logic.
    

JSON Representation:
[
  {
    "title": "Dune",
    "author": "Frank Herbert",
    "pages": 412
  },
  {
    "title": "The Hobbit",
    "author": "J.R.R. Tolkien",
    "pages": 310
  },
  {
    "title": "Pride and Prejudice",
    "author": "Jane Austen",
    "pages": 432
  }
]
    

Conclusion

Lists of objects are a cornerstone of Python programming.

They bridge simple data storage and complex modeling.

You learned how to create, access, modify, sort, and filter them.

Remember, the objects in your list can be as simple or complex as your class defines.

Mastering this skill unlocks your ability to handle real-world data.

Combine it with inheritance and advanced attribute management for even more power.

Practice by modeling something from your own environment.

Create a list, and make your code work for you.