Python Crash Course Lists Methods! #3
Hi Everyone, Dumping more notes today for my own reference! Need more practice with these. 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. motorcycles[0] = 'ducati' 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.append('ducati') Inserting Elements into a List - You can add a new element at any position in your list by using the insert() method. You do this by specifying the index of the new element and the value of the new item. motorcycles