How to Concatenate and Slice Lists in Python

In this tutorial, I’m going to show you how to concatenate and slice lists in Python. I’ll start with a simple concatenation of two lists, and then review the different ways to slice lists in Python.

How to Concatenate Lists in Python

You can use the plus sign (‘+’) in order to concatenate lists in Python. Here is the general structure that you may apply in Python to perform the concatenation:

Combined_List = List1 + List2

Let’s see how this structure can be used by reviewing a simple example where you’ll concatenate these two lists:

 

Names1 = ['Jon', 'Bob', 'Mila', 'Ed', 'Jill']
Names2 = ['Ella','Mark','Jeff','Emma','Mia']

 

Now, use the + sign to concatenate the two lists together:

Combined_Names_List = Names1 + Names2

And this is how the full Python code would look like:

 

Names1 = ['Jon', 'Bob', 'Mila', 'Ed', 'Jill']
Names2 = ['Ella','Mark','Jeff','Emma','Mia']

Combined_Names_List = Names1 + Names2

print (Combined_Names_List)

 

Once you run the Python code, you’ll get:

 

How to Concatenate or Slice Lists in Python

 

If you’re using the pandas library in Python, you may want to check the following source that further explains how to concatenate values in Python using pandas.

In the next section, I’ll review the steps needed to slice lists in Python.

How to Slice Lists in Python

Let’s say that you created a list in Python using the following structure:

ListName = [Item1, Item2, Item3,….]

Each item within the list created will have an index (starting from zero).

For example, Item1 has the index of 0, Item2 has the index of 1, Item3 has the index of 2 and so on…

You can then use the index value to slice your list.

Let’s now review an example to demonstrate this concept.

Example of slicing lists in Python

Imagine that you created a single list called the ‘Combined_Names_List’ which contains 10 items (e.g., 10 names):

 

Combined_Names_List = ['Jon', 'Bob', 'Mila', 'Ed', 'Jill','Ella','Mark','Jeff','Emma','Mia']

 

Your goal is to slice that list into 2 lists:

  • Names1 – this list will contain the first 4 names
  • Names2 – this list will contain the remaining 6 names

How would you then slice that list?

Here are 3 ways to slice the list in Python:

(1) Slicing the list using [start item: end item]

Here is the structure that you can use to slice a list:

[index of the start item: index of the end item]

For our example:

  • To get the 4 first items, inclusive of the 4th item, you’ll need to use:
    Names1 = Combined_Names_List[0:4]
  • And in order to get the remaining 6 items:
    Names2 = Combined_Names_List[4:10]

And the full Python code is:

 

Combined_Names_List = ['Jon', 'Bob', 'Mila', 'Ed', 'Jill','Ella','Mark','Jeff','Emma','Mia']

Names1 = Combined_Names_List[0:4] # get the 4 first items, inclusive of the 4th item
Names2 = Combined_Names_List[4:10] # get all the items, starting from the 5th item to the 10th item, inclusive

print (Names1)
print (Names2)

 

This is what you’ll see once you run the Python code:

Slice Lists in Python

(2) Slicing the list using [:end item] and [start item:]

You can also slice a list in Python by using:

[:index of the end item] and [index of the start item:]

And for our example:

  • To get all the items up to the 4th item, inclusive:
    Names1 = Combined_Names_List[:4]
  • To get all items after the 4th item:
    Names2 = Combined_Names_List[4:]

This is the syntax that you may use in Python:

 

Combined_Names_List = ['Jon', 'Bob', 'Mila', 'Ed', 'Jill','Ella','Mark','Jeff','Emma','Mia']

Names1 = Combined_Names_List[:4] # get all the items up to the 4th item, inclusive
Names2 = Combined_Names_List[4:] # get all items after the 4th item

print (Names1)
print (Names2)

 

Here is the output from Python, which is identical to the first case you saw before:

Slice Lists in Python

(3) Slicing the list using [:negative item] and [negative item:]

Finally, you may slice your list in Python using:

[:negative item index] and [negative item index:]

For our case:

  • To get all items except for the last 6 items:
    Names1 = Combined_Names_List[:-6]
  • To get the last 6 items:
    Names2 = Combined_Names_List[-6:]

Here is the Python code:

 

Combined_Names_List = ['Jon', 'Bob', 'Mila', 'Ed', 'Jill','Ella','Mark','Jeff','Emma','Mia']

Names1 = Combined_Names_List[:-6] # get all items except for the last 6 items
Names2 = Combined_Names_List[-6:] # get the last 6 items

print (Names1)
print (Names2)

 

You’ll get the same results as in the previous two cases:

Slice Lists in Python