H a n d s o n, p r o j e c t b a s e d
Modifying Elements in a List
Download 4.21 Mb. Pdf ko'rish
|
Python Crash Course, 2nd Edition
- Bu sahifa navigatsiya:
- Adding Elements to a List
- Appending Elements to the End of a List
Modifying Elements in a List
The syntax for modifying an element is similar to the syntax for accessing an element in a list. To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have. Introducing Lists 37 For example, let’s say we have a list of motorcycles, and the first item in the list is 'honda' . How would we change the value of this first item? motorcycles.py u motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) v motorcycles[0] = 'ducati' print(motorcycles) The code at u defines the original list, with 'honda' as the first element. The code at v changes the value of the first item to 'ducati' . The output shows that the first item has indeed been changed, and the rest of the list stays the same: ['honda', 'yamaha', 'suzuki'] ['ducati', 'yamaha', 'suzuki'] You can change the value of any item in a list, not just the first item. Adding Elements to a List You might want to add a new element to a list for many reasons. For example, you might want to make new aliens appear in a game, add new data to a visualization, or add new registered users to a website you’ve built. Python provides several ways to add new data to existing lists. Appending Elements to the End of a List The simplest way to add a new element to a list is to append the item to the list. When you append an item to a list, the new element is added to the end of the list. Using the same list we had in the previous example, we’ll add the new element 'ducati' to the end of the list: motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) u motorcycles.append('ducati') print(motorcycles) The append() method at u adds 'ducati' to the end of the list without affecting any of the other elements in the list: ['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha', 'suzuki', 'ducati'] 38 Chapter 3 The append() method makes it easy to build lists dynamically. For example, you can start with an empty list and then add items to the list using a series of append() calls. Using an empty list, let’s add the elements 'honda' , 'yamaha' , and 'suzuki' to the list: motorcycles = [] motorcycles.append('honda') motorcycles.append('yamaha') motorcycles.append('suzuki') print(motorcycles) The resulting list looks exactly the same as the lists in the previous examples: ['honda', 'yamaha', 'suzuki'] Building lists this way is very common, because you often won’t know the data your users want to store in a program until after the program is running. To put your users in control, start by defining an empty list that will hold the users’ values. Then append each new value provided to the list you just created. Download 4.21 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling