List Slices

The Dream Team

Suppose that you were playing a game that allowed you to look at the rosters of your favorite soccer team.

There's a mode in which you can add a player from any other soccer team to your favorite team's roster and play online with your friends.

If you are creating the game you can store the original roster in a list called original_roster. However your game is a 3-on-3 soccer game and only a subset of your roster can be on the field at one time.

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.

Creating Slices


                        players = ['lionel messi', 'euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
                        # Create a slice of the first 2 players.
                        print(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é', 'gauss']
                        ['euler', 'pelé', 'gauss', 'cristiano ronaldo', 'goat']
              
            

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 using a Slice

There are times when you want to make an exact copy of a list and start modifying the new list.


            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 new_list=original_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.