Abstract
Lists help programmers manage larger amounts of data by allowing several (or even many) values to be stored in one variable. As a program executes it can transform a list (and the values within it) in numerous ways. Creatively applying these transformations, together with loops and decision making constructs, allows complex problems involving large amounts of data to be solved with a small number of variables. The specific list concepts explored in this chapter include:
-
Creating a new list,
-
Modifying a list by appending, inserting, updating and deleting elements,
-
Searching a list for a value,
-
Displaying some or all of the elements in a list, and
-
Writing a function that takes a list as an argument or returns a list as a result.
The chapter concludes with 26 exercises that allow the reader to use these concepts to solve a variety of problems.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
The len function returns 0 if the list passed to it is empty.
- 2.
Methods can also be applied to a list literal enclosed in square brackets using the same syntax, but there is rarely a need to do so.
- 3.
A list can only be sorted if all of the elements in it can be compared to one another with the less than operator. The less than operator is defined for many Python types include integers, floating-point numbers, strings, and lists, among others.
Author information
Authors and Affiliations
Corresponding author
Rights and permissions
Copyright information
© 2019 Springer Nature Switzerland AG
About this chapter
Cite this chapter
Stephenson, B. (2019). Lists. In: The Python Workbook. Texts in Computer Science. Springer, Cham. https://doi.org/10.1007/978-3-030-18873-3_5
Download citation
DOI: https://doi.org/10.1007/978-3-030-18873-3_5
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-030-18872-6
Online ISBN: 978-3-030-18873-3
eBook Packages: Computer ScienceComputer Science (R0)