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.
Chapter 1: Python Basics
An introduction to the basic concepts of Python. Learn how to use Python interactively and by using a script. Create your first variables and acquaint yourself with Python's basic data types.
Exercise 1: Hello Python!Exercise 2: Your first Python codeExercise 3: Any comments?Exercise 4: Python as a calculatorExercise 5: Variables and TypesExercise 6: Variable AssignmentExercise 7: Calculations with variablesExercise 8: Other variable typesExercise 9: Operations with other typesChapter 2: Python Lists
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 listsChapter 3: Functions and Packages
You'll learn how to use functions, methods, and packages to efficiently leverage the code that brilliant Python developers have written. The goal is to reduce the amount of code you need to solve challenging problems!
Exercise 1: FunctionsExercise 2: Familiar functionsExercise 3: Help!Exercise 4: Multiple argumentsExercise 5: MethodsExercise 6: String MethodsExercise 7: List MethodsExercise 8: List Methods (2)Exercise 9: PackagesExercise 10: Import packageExercise 11: Selective importExercise 12: Different ways of importingChapter 4: NumPy
NumPy is a fundamental Python package to efficiently practice data science. Learn to work with powerful tools in the NumPy array, and get started with data exploration.
Exercise 1: NumPyExercise 2: Your First NumPy ArrayExercise 3: Baseball players' heightExercise 4: NumPy Side EffectsExercise 5: Subsetting NumPy ArraysExercise 6: 2D NumPy ArraysExercise 7: Your First 2D NumPy ArrayExercise 8: Baseball data in 2D formExercise 9: Subsetting 2D NumPy ArraysExercise 10: 2D ArithmeticExercise 11: NumPy: Basic StatisticsExercise 12: Average versus medianExercise 13: Explore the baseball dataWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.