Python language basics 41: 4 ways to insert new elements into a collection

Introduction

In the previous post we looked at negative indexes for collections. We saw how they could be used to access elements from the tail of the list. An index of -1 will help you get to the last element of a collection regardless of its size.

In this post we’ll look at 4 ways to insert elements into a collection.

Element insertion

Consider the following cities collection:

cities = ["Stockholm", "Budapest", "Berlin", "Paris", "Birmingham", "Zürich"]

The insert function

If you want to insert a new element just after “Berlin”, i.e. at position 4 – or 3 as indexes are 0-based, then you can use the insert function. This function accepts an index where to put the new item and an object to be inserted:

cities.insert(3, "Oslo")

Here’s the revised list:

[‘Stockholm’, ‘Budapest’, ‘Berlin’, ‘Oslo’, ‘Paris’, ‘Birmingham’, ‘Zürich’]

The append function

If you want to add an item to the end of the list then use the append method:

cities.append("London")

…which will extend the cities list accordingly:

[‘Stockholm’, ‘Budapest’, ‘Berlin’, ‘Paris’, ‘Birmingham’, ‘Zürich’, ‘London’]

The + and += operators

You can insert multiple elements to the end of list using the + and += operators:

cities = ["Stockholm", "Budapest", "Berlin", "Paris", "Birmingham", "Zürich"]
more_cities = ["Barcelona", "Helsinki", "Copenhagen", "Moscow"]
joined_cities = cities + more_cities

…where joined_cities will include all city names as expected:

[‘Stockholm’, ‘Budapest’, ‘Berlin’, ‘Paris’, ‘Birmingham’, ‘Zürich’, ‘Barcelona’, ‘Helsinki’, ‘Copenhagen’, ‘Moscow’]

If you want to append the “more_cities” collection to “cities” instead of building a new collection then you can use the += operator:

cities = ["Stockholm", "Budapest", "Berlin", "Paris", "Birmingham", "Zürich"]
more_cities = ["Barcelona", "Helsinki", "Copenhagen", "Moscow"]
cities += more_cities

‘cities’ will become…

[‘Stockholm’, ‘Budapest’, ‘Berlin’, ‘Paris’, ‘Birmingham’, ‘Zürich’, ‘Barcelona’, ‘Helsinki’, ‘Copenhagen’, ‘Moscow’]

The extend function

The extend function has the same effect as the += operator:

cities = ["Stockholm", "Budapest", "Berlin", "Paris", "Birmingham", "Zürich"]
more_cities = ["Barcelona", "Helsinki", "Copenhagen", "Moscow"]
cities.extend(more_cities)

Again, cities will include all city names:

[‘Stockholm’, ‘Budapest’, ‘Berlin’, ‘Paris’, ‘Birmingham’, ‘Zürich’, ‘Barcelona’, ‘Helsinki’, ‘Copenhagen’, ‘Moscow’]

In the next post we’ll look at a couple of ways to remove an element from a list.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: