Extend a list
If you can change elements in a list, you sure want to be able to add elements to it, right? You can use the +
operator:
x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
You just won the lottery, awesome! You decide to build a poolhouse and a garage. Can you add the information to the areas
list?
This is a part of the course
“Introduction to Python”
Exercise instructions
- Use the
+
operator to paste the list["poolhouse", 24.5]
to the end of theareas
list. Store the resulting list asareas_1
. - Further extend
areas_1
by adding data on your garage. Add the string"garage"
and float15.45
. Name the resulting listareas_2
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
"bedroom", 10.75, "bathroom", 10.50]
# Add poolhouse data to areas, new list is areas_1
areas_1 = ____
# Add garage data to areas_1, new list is areas_2
areas_2 = ____
This exercise is part of the course
Introduction to Python
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
Learn to store, access, and manipulate data in lists: the first step toward efficiently working with huge amounts of data.
Exercise 1: Python ListsExercise 2: Create a listExercise 3: Create lists with different typesExercise 4: List of listsExercise 5: Subsetting ListsExercise 6: Subset and conquerExercise 7: Slicing and dicingExercise 8: Subsetting lists of listsExercise 9: Manipulating ListsExercise 10: Replace list elementsExercise 11: Extend a listExercise 12: Delete list elementsExercise 13: Inner workings of listsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.