3 Lists
3 Lists
1.3. Lists
• What Is a List?
• Changing, Adding, and Removing Elements
• Organizing a List
• Avoiding index errors when working with lists
What Is a List? 3
• A list stores a series of items in a particular order.
• Lists allow you to store sets of information in one place, whether you
have just a few items or millions of items.
• Lists are one of Python's most powerful features readily accessible to
new programmers, and they tie together many important concepts in
programming.
Defining a list
Use square brackets ([]) to define a list, and use commas to separate
individual items in the list.
• Use plural names for lists, to make your code easier to read.
What Is a List? 4
Accessing elements
• Individual elements in a list are accessed according to their position, called
the index.
• To get a particular element, write the list's name and then the element's
index in square brackets.
Start End
Index
Negative index
Practice 5
3-1,...,3-3
Changing, Adding, and Removing Elements 6
Changing
Changing, Adding, and Removing Elements 7
Adding
Changing, Adding, and Removing Elements 8
Changing, Adding, and Removing Elements 9
Removing
Changing, Adding, and Removing Elements 10
Removing
Practice 11
3-4,…, 3.7
Organizing a List 12
3-8,…, 3.10
Avoiding Index Errors When Working with Lists 15
Practice 16
3-11