Skip to content
Home » Appending Elements to Lists in Python

Appending Elements to Lists in Python

Spread the love

Today we’ll learn how to append elements to lists using the append method.

Here’s the video version of this article:

Lists don’t have a fixed size. They can grow at runtime. You use append to add new elements:

>>> names = ['Alice', 'Bernard', 'Steve', 'Emma', 'Sam']
>>> names.append("Jenny")
>>> names
['Alice', 'Bernard', 'Steve', 'Emma', 'Sam', 'Jenny']

You also use the append method to add a list as a sublist:

>>> authors = ["Austen", ["Smith", "Garson"], "Bennet"]
>>> authors.append(["Jackson", "Warwick"])
>>> authors
['Austen', ['Smith', 'Garson'], 'Bennet', ['Jackson', 'Warwick']]

You can define empty lists. Then you can add elements to them:

>>> scores = []
>>> scores
[]

>>> scores.append(5)
>>> scores.append(14)
>>> scores
[5, 14]

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love
Tags:

Leave a Reply