Append items to a List in a Class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Append items to a List in a Class in Python #

To append items to a list in a class:

  1. Initialize the list in the class's __init__() method.
  2. Define a method that takes one or more items and appends them to the list.
main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary self.tasks = [] # 👈️ initialize list def add_task(self, task): self.tasks.append(task) return self.tasks bob = Employee('Bobbyhadz', 100) bob.add_task('develop') bob.add_task('ship') print(bob.tasks) # 👉️ ['develop', 'ship']

We initialized the tasks list as an instance variable in the class's __init__() method.

Instance variables are unique to each instance you create by instantiating the class.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary self.tasks = [] # 👈️ initialize list def add_task(self, task): self.tasks.append(task) return self.tasks alice = Employee('Alice', 1000) alice.add_task('design') alice.add_task('test') print(alice.tasks) # 👉️ ['design', 'test'] bob = Employee('Bobbyhadz', 100) bob.add_task('develop') bob.add_task('ship') print(bob.tasks) # 👉️ ['develop', 'ship']

The two instances have separate tasks lists.

You can also use class variables instead of instance variables.

Class variables are shared by all instances of the class.

main.py
class Employee(): # 👇️ class variable tasks = [] def __init__(self, name, salary): self.name = name self.salary = salary @classmethod def add_task(cls, task): cls.tasks.append(task) return cls.tasks Employee.add_task('develop') Employee.add_task('ship') print(Employee.tasks) # 👉️ ['develop', 'ship'] alice = Employee('Alice', 1000) print(alice.tasks) # 👉️ ['develop', 'ship'] bob = Employee('Bobbyhadz', 100) print(bob.tasks) # 👉️ ['develop', 'ship']

The tasks variable is a class variable, so it is shared by all instances.

We marked the add_task() method as a class method. The first argument class methods get passed is the class.

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

However, something you might often have to do is append multiple items to the list.

You can use the list.extend() method to append the items of an iterable to a list.

main.py
class Employee(): def __init__(self, name, salary): # 👇️ instance variables (unique to each instance) self.name = name self.salary = salary self.tasks = [] # 👈️ initialize list def add_tasks(self, iterable_of_tasks): self.tasks.extend(iterable_of_tasks) return self.tasks bob = Employee('Bobbyhadz', 100) bob.add_tasks(['develop', 'test', 'ship']) print(bob.tasks) # 👉️ ['develop', 'test', 'ship']

We used the list.extend() method to append multiple values to the tasks list.

The list.extend method takes an iterable (such as a list or a tuple) and extends the list by appending all of the items from the iterable.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.