Lists

Introduction to Python for Developers

Jasmin Ludolf

Senior Data Science Content Developer

Problem

# Variables of ingredient names
ingredient_one = "pasta"
ingredient_two = "tomatoes"
ingredient_three = "garlic"
ingredient_four = "basil"
ingredient_five = "olive oil"
ingredient_six = "salt"
Introduction to Python for Developers

Lists to the rescue!

  • List = store multiple values in a single variable
    • Can contain any combination of data types

 

# List of ingredients
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]
# List of ingredients using variables as values
ingredients = [ingredient_one, ingredient_two, ingredient_three,
               ingredient_four, ingredient_five, ingredient_six]
Introduction to Python for Developers

Checking the data type

# Check the data type of a list
print(type(ingredients))
<class 'list'>
Introduction to Python for Developers

Accessing elements of a list

# Print all values in the list variable
print(ingredients)
['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt']
  • Lists are ordered and indexed
    • Python assigns index starting from zero for the first element
Introduction to Python for Developers

Accessing elements of a list

  • List = []
  • Access an element = a_list[index]
ingredients = ["pasta", "tomatoes",
"garlic", "basil", "olive oil", "salt"]

# Get the value at the first index
print(ingredients[0])
pasta
# Get the fourth element
print(ingredients[3])
basil
Introduction to Python for Developers

Finding the last element in a list

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Get the last element of a list
print(ingredients[5])
salt
# Get the last element of a list
print(ingredients[-1])
salt
Introduction to Python for Developers

Accessing multiple elements

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Access the second and third elements
print(ingredients[1:3])
["tomatoes", "garlic"]
  • [first_element:last_element + 1]
  • Add one to the last element's index because:
    • Python returns everything up to but not including that index
Introduction to Python for Developers

Accessing multiple elements

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Access all elements from the third index onwards
print(ingredients[3:])
['basil', 'olive oil', 'salt']
# Get the first three elements
print(ingredients[:3])
['pasta', 'tomatoes', 'garlic']
Introduction to Python for Developers

Alternating access

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Access every second element print(ingredients[::2])
['pasta', 'garlic', 'olive oil']]
  • Returns items at indexes zero, two, and four
# Access every third element, starting at the second
print(ingredients[1::3])
['tomatoes', 'olive oil']
  • Returns items at indexes one and four
Introduction to Python for Developers

Let's practice!

Introduction to Python for Developers

Preparing Video For Download...