Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Using Python Lists

Lists are one of the fundamental data types that we use in Python for solving real problems. Being able to manipulate lists and access items is necessary for effective programming. In this hands-on lab, we'll be working through some exercises demonstrating that we understand how to add, remove, modify, and read items from lists in Python. We'll perform actions on a list to meet some checkpoint requirements provided to us within a Python file. To feel comfortable completing this lab we'll want to know how to do the following: * Working with list literals: Watch the "Lists" video from the Certified Entry-Level Python Programmer Certification course. * Using list functions and methods: Watch the "List Functions and Methods" video from the Certified Entry-Level Python Programmer Certification course.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 45m
Published
Clock icon Nov 26, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create a users List and Add Initial Items

    Note: The using-lists.py file exists in the /home/cloud_user/ directory. If you do not see the file, please give the lab another minute to finish provisioning. If you prefer to use your own local Python editor, you can also access the initial using-lists.py file in the lab's GitHub repository.

    Our first few tasks require us to create the users variable that we're going to work with throughout the lab and then add some information to it. Here's how we complete the first task:

    using-lists.py (partial)

    # 1) Set the users variable to be an empty list
    users = []
    
    assert users == [], f"Expected `users` to be [] but got: {repr(users)}"
    

    Now if we run the file we should see the error for the second task:

    $ python3.7 using-lists.py
    Traceback (most recent call last):
      File "./using-lists.py", line 8, in <module>
        assert users == ['kevin', 'bob', 'alice'], f"Expected `users` to be ['kevin', 'bob', 'alice'] but got: {repr(users)}"
    AssertionError: Expected `users` to be ['kevin', 'bob', 'alice'] but got: []
    

    This error shows us that we need to add values to the list before we can continue. The task also specifies that we shouldn't just reassign the users variable. Here's an example solution to this:

    using-lists.py (partial)

    # 2) Add 'kevin', 'bob', and 'alice' to the users list in that order without reassigning the variable.
    users.append('kevin')
    users.append('bob')
    users.append('alice')
    
    assert users == ['kevin', 'bob', 'alice'], f"Expected `users` to be ['kevin', 'bob', 'alice'] but got: {repr(users)}"
    
  2. Challenge

    Remove bob and Create rev_users List

    For tasks 3 and 4 we need to remove the bob value and then create a new reversed version of the new list. Here's an example solution for both of these tasks:

    using-lists.py (partial)

    # 3) Remove 'bob' from the `users` list without reassigning the variable.
    del users[1]
    
    assert users == ['kevin', 'alice'], f"Expected `users` to be ['kevin', 'alice'] but got: {repr(users)}"
    
    # 4) Reverse the users list and assign the result to `rev_users`
    rev_users = list(reversed(users))
    
    assert rev_users == ['alice', 'kevin'], f"Expected `rev_users` to be ['alice', 'kevin'] but got: {repr(rev_users)}"
    

    The del statement will allow us to remove an item at a specific index to complete task three. For task four, we'll use the reversed function and then cast the result of that back to a list using the list function.

  3. Challenge

    Insert melody to users and Concatenate Lists

    For tasks five and six we'll be adding more information to our users list. To add a single item at the first index we'll use the insert method. To add three new items to the end of the list we'll use list concatenation and reassign the users variable.

    using-lists.py (partial)

    # 5) Add the user 'melody' to users where 'bob' used to be.
    users.insert(1, 'melody')
    
    assert users == ['kevin', 'melody', 'alice'], f"Expected `users` to be ['kevin', 'melody', 'alice'] but got: {repr(users)}"
    
    # 6) Add the users 'andy', 'wanda', and 'jim' to the users list using a single command
    users += ['andy', 'wanda', 'jim']
    
    assert users == ['kevin', 'melody', 'alice', 'andy', 'wanda', 'jim'], f"Expected `users` to be ['kevin', 'melody', 'alice', 'andy', 'wanda', 'jim'] but got: {repr(users)}"
    
  4. Challenge

    Slice`users to Return 3rd and 4th Elements

    For the final task, we'll extract a subsection from the users list to create the new center_users list. We can extract the third and fourth items from the list by slicing from 2 to 4.

    using-lists.py (partial)

    # 7) Slice the users lists to return the 3rd and 4th items and assign the result to `center_users`
    center_users = users[2:4]
    
    assert center_users == ['alice', 'andy'], f"Expected `users` to be ['alice', 'andy'] but got: {repr(center_users)}"
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans