Python List Operations: Concatenation, Multiplication, Slicing & del

Python List Operations: Concatenation, Multiplication, Slicing & del

Lists and strings have a lot in common. They are both sequences and, like pythons, they get longer as you feed them. Like a string, we can concatenate and multiply a Python list.

Python List Concatenation & Multiplication

Old MacDonald had a farm, E-I-E-I-O. And on this farm there was a python, E-I-E-I-O. At the prompt, create a Python list with an item, ‘farm’:

That’s not what we wanted. Our string, EIEIO, was split into individual items and then added to our list. Why? Because strings are iterable. So how do you add strings to a Python list? With brackets.

Try adding a number to a list…

How do we add integers to lists? Just like we did with strings.

Lastly, what do you think will happen when you run this?

We can also perform multiplication on the entire list or individual items in the list by index.

For example:


And by index:

Eeee! How exciting!

You win! Time for dessert.

Slicing a Python List

Care for a bite of hedgehog? A hedgehog slice is a cake made of layers of chocolate and biscuit. It’s delicious. A group of hedgehogs is called an array. Let’s create a list called ‘array’ and populate it with hedgehogs.

Using an index we can easily see what the first item is:

But what if we want everything but the first item? We slice it, just like a cake.

And if we only want the middle?

Deleting Items from a Python List

Because our array is populated with prickly and disagreeable hedgehogs, their spines are irritating one another. So we need to clear out a few items.

Use del to remove one item, or a slice of items

Meanwhile, Back at the Ranch…

This little piggy learned Python list operations,

This little piggy stayed home,

This little piggy ate a hedgehog slice,

And this little piggy had none.

And this little piggy went wee wee wee all the way to the next Python list lesson.

Python List Operations: Concatenation, Multiplication, Slicing & del was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid