Python Logo

List Slices

List Slices

A list slice is a subset of an existing list. That is, it's a smaller list made from a bigger list. We can create a list slice easily with the following syntax:

list[start:stop]

Please note that when taking a slice of a list the slice only includes indices that are less than the stop index. Here is a short example of some various ways to make slices:


                    players = ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
                    # Create a slice of the first 2 players.
                    players[0:2]
                    # Create a slice from the beginning of the list to index 3
                    print(players[:4])
                    # Create a slice starting from index 1 to the end of the list.
                    print(players[1:])
                
          
                    ['lionel messi', 'euler']
                    ['lionel messi', 'euler', 'pelé']
          
        

Looping Through a Slice

You can loop through a slice and apply the usual methods on them as shown below:


          players = ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
          # Print the first three players in the list in their own line
          for player in players[:3]:
            print(player.title())
        
          
            Lionel Messi
            Euler
            Pelé
          
        

Copying a List

There are times when you want to make an exact copy of a list and start modifying the new list. An example is if you make a website that lets people list their favorite G.O.A.T soccer players and it lets you copy someone else's list and modify it to your liking.


        players = ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
        # Copy the players list
        my_players = players[:]
        # Print the Lists:
        print("My friend's favorite players are:")
        print(players)
        print("My favorite players are:")
        print(my_players)
        

          My friend's favorite players are:
          ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
          My favorite players are:
          ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
        

We can use any of the methods we've learned to change the lists independently.


          players = ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
        # Copy the players list
        my_players = players[:]
        # Add a new player to your friend's list:
        players.append('mbappe')
        # Add a new player to your own list:
        my_players.append('mario gotze')
        # Print the Lists:
        print("My friend's favorite players are:")
        print(players)
        print("My favorite players are:")
        print(my_players)
        

          My friend's favorite players are:
          ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat', 'mbappe']
          My favorite players are:
          ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat', 'mario gotze']
        

Common Mistake in Copying List

One might think that copying a list is as simple as setting it equal to another existing list like original_list=new_list, however this actually causes the new_list variable to point to original_list. If you need to copy a list, please be sure to do it using a list slice.