DEV Community

I'm Just a Guy with a Computer
I'm Just a Guy with a Computer

Posted on

Adding Values to Lists with the append() and insert() Methods in Python.

To add new values to a list, use the append() and insert() methods. Enter the following into the interactive shell to call the append() method on a list value stored in the variable cars.

>>> cars = ['Benz','Audi','Lexus','Jeep']
>>> cars.append('BMW')
>>> cars
['Benz', 'Audi', 'Lexus', 'Jeep', 'BMW']
>>>
Enter fullscreen mode Exit fullscreen mode

The previous append() method call adds the argument to the end ofthe list. The insert() method can insert a value at any index in the list.The first argument to insert() is the index for the new value, and the second argument is the new value to be inserted. Enter the following into the interactive shell.

>>> Languages = ['Python','Javascript','Php','Rust']
>>> Languages.insert(2,'Golang')
>>> Languages
['Python', 'Javascript', 'Golang', 'Php', 'Rust']
>>> 
Enter fullscreen mode Exit fullscreen mode

InClusion.
Methods belong to a single data type .The append() and insert() methods are list methods and can be called on list values , not on other values such as strings or integers.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more