top of page

Understanding Lists in Python

Understanding and effectively using lists is a fundamental skill in Python programming. Lists are versatile and can be used to store a collection of items, which can be of different types like integers, floats, strings, and even other lists.



What is a List?


A list is just like that treasure chest. It can hold all sorts of things. Instead of gold coins and chocolate bars, it might hold numbers, words, or even other lists!


Just like how you can dig around in the treasure chest to find the rubber ducky, you can "dig" into a Python list to find any item you want. And the best part? You can always add more treasure to your chest, or if you decide you don't like rubber ducks anymore, you can take it out.


In Python, a list is an ordered collection of items. Lists are mutable, meaning their contents can be changed after creation. They are defined by enclosing a comma-separated sequence of items in square brackets []. For example:


my_list = [1, 2, 3, 'apple', 'banana']

Now that we've explained the concept, let's dive into two basic exercises to help you understand lists better:


Exercise 1 - List Manipulation

  • Create a list of numbers from 1 to 10.

  • Add the number 11 to the end of the list.

  • Remove the number 5 from the list.

  • Print the final list.



# Create a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Add 11 to the end of the list
numbers.append(11)

# Remove 5 from the list
numbers.remove(5)

# Print the final list
print(numbers)


Exercise 2 - List Slicing

  • Create a list of numbers from 1 to 10.

  • Print the first 5 elements of the list.

  • Print the last 3 elements of the list.

  • Print elements 3 to 7 from the list.



# Create a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Print the first 5 elements
print(numbers[:5])

# Print the last 3 elements
print(numbers[-3:])

# Print elements 3 to 7
print(numbers[2:7])

Your Turn

Here's an additional exercise to apply what you've learned about lists:


Working with Multiple Lists

  • Create two separate lists: one with your favorite fruits and another with your favorite vegetables.

  • Concatenate (combine) the two lists into a single list named favorites.

  • Sort the favorites list in alphabetical order.

  • Print the first three items from the favorites list.

  • Print the last item from the favorites list.

The Python sort() method can be used to sort a list in place, and list slicing can be used to select specific items from the list. Try to solve this exercise and check your understanding of Python lists.


Please work on the given exercise using Repl.it, an online code editor that supports Python. Once you've completed the exercise, kindly share your Repl.it link with us for review. This will allow us to check your code and provide feedback wherever necessary.


Hints:

# Create the fruits and vegetables lists


# Concatenate the lists


# Sort the combined list


# Print the first three items


# Print the last item


Comments


bottom of page