Lesson 25. Working with lists

 

After reading lesson 25, you’ll be able to

  • Build Python lists
  • Add items, remove items, and modify items in Python lists
  • Perform operations on list elements

Mutable data types are types of objects on which you can perform operations, such that the object’s value is modified; the modification is done in place, so to speak, so no copy of the object is made. There’s a need for data types that are mutable, especially when working with large amounts of data; it’s more efficient to modify data stored directly instead of copying it into a new object with every operation.

Instead of creating new objects, it’s sometimes useful to reuse an object and modify its value. This is especially true when you have an object that represents an ordered collection of other objects. Most programming languages have a data type to represent this type of ordered collection that you can mutate. In Python, this is called a list. A list is a new type of object that you haven’t seen before. It represents an ordered collection of other object types. Among many others, you can have lists of numbers, lists of strings, or even a list of a mix of object types.

25.1. Lists vs. tuples

 

25.2. Creating lists and getting elements at specific positions

 
 

25.3. Counting and getting positions of elements

 
 
 
 

25.4. Adding items to lists: append, insert, and extend

 

25.5. Removing items from a list: pop

 
 
 
 

25.6. Changing an element value

 
 
 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage