Created by - Robert Kotaki
Introduction to Lists in PythonIn this chapter and the next, we will learn about lists in Python. Lists are a powerful feature in Python that allow you to store and manipulate sets of information in one place. They are especially useful when dealing with large amounts of data.What is a List?A list is a collection of items in a specific order. You can put anything you want in a list, and the items in a list do not have to be related in any way. In Python, you can create a list using square brackets ([]) and separating the individual items with commas. It's a good idea to use a plural name for the list, such as "bicycles" or "names".Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) Output:cssCopy code['trek', 'cannondale', 'redline', 'specialized'] Accessing Elements in a ListLists are ordered collections, which means you can access any element in a list by telling Python the position or index of the item you want. In Python, index positions start at 0, not 1.To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0]) Output:Copy codetrek You can also use string methods, such as title(), on individual items in the list.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0].title()) Output:Copy codeTrek Negative index values can also be used to access elements in a list, with -1 representing the last element in the list, -2 representing the second-to-last element, and so on.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[-1]) Output:Copy codespecialized Using Individual Values from a ListYou can use individual values from a list just like any other variable. For example, you can use f-strings to create a message based on a value from a list.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] message = f"My first bicycle was a {bicycles[0].title()}." print(message) Output:cssCopy codeMy first bicycle was a Trek. Summary:A list is a collection of items in a specific order.Lists are created using square brackets ([]) and separating individual items with commas.Individual items in a list can be accessed using their index position.Index positions start at 0, not 1.Negative index values can be used to access elements from the end of the list.Individual values from a list can be used just like any other variable.
More detailsPublished - Tue, 11 Apr 2023
Created by - Robert Kotaki
Lists are dynamic, meaning that you can add, remove or modify elements as needed during the program’s execution. In Python, modifying a list element is done by assigning a new value to an element using the index operator [].This code will output ['ducati', 'yamaha', 'suzuki']. We've changed the first item of the list motorcycles from 'honda' to 'ducati'.To add an element to a list, Python provides several ways to add new data to existing lists.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. The append() method is used for this operation.This code will output ['honda', 'yamaha', 'suzuki', 'ducati']. We've added the new element 'ducati' to the end of the 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.This code will output ['ducati', 'honda', 'yamaha', 'suzuki']. We've inserted the value 'ducati' at the beginning of the list.To remove an item from a list, you can use the del statement or the remove() method.If you know the position of the item you want to remove from a list, you can use the del statement.This code will output ['yamaha', 'suzuki']. We've removed the first item, 'honda', from the list motorcycles.If you know the value of the item you want to remove from a list, you can use the remove() method.This code will output ['honda', 'suzuki']. We've removed the item 'yamaha' from the list motorcycles.
More detailsPublished - Tue, 11 Apr 2023
Created by - Robert Kotaki
Introduction to ListsSorting a List Permanently with the sort() MethodpythonCopy codecars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars) cars.sort(reverse=True) print(cars) sorted() function lets you display your list in a particular order without affecting the actual order of the list.We can display a list of cars in alphabetical or reverse-alphabetical order temporarily.Example code:Printing a List in Reverse Order with the reverse() MethodpythonCopy codecars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) cars.reverse() print(cars) len() function returns the length of a list.We can use len() to identify the number of items in a list.Example code:
More detailsPublished - Tue, 11 Apr 2023
Created by - Robert Kotaki
Avoiding Index Errors When Working with ListsWhen working with lists in Python, it's important to understand how indexing works to avoid index errors. This lecture will cover the basics of indexing in lists and provide some tips to avoid common errors.Indexing in ListsLists are indexed starting at 0, not 1.To access an item in a list, you use square brackets [] and provide the index of the item you want to access.If you provide an index that is out of range for the list, you will get an index error.To access the last item in a list, you can use the index -1.ExamplesLet's create a list of motorcycles and try to access the fourth item:scssCopy codemotorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[3]) This will result in an index error because there is no item at index 3. To fix this error, we need to remember that indexing starts at 0, so the third item in the list is at index 2. We can correct the code by changing the index to 2:scssCopy codemotorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[2]) This will correctly print the third item in the list, 'suzuki'.Let's try to access the last item in the list using the index -1:scssCopy codemotorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[-1]) This will correctly print the last item in the list, 'suzuki'.Finally, let's try to access the last item in an empty list:scssCopy codemotorcycles = [] print(motorcycles[-1]) This will result in an index error because there are no items in the list.Tips to Avoid Index ErrorsRemember that indexing starts at 0, not 1.Use the index -1 to access the last item in a list.If you get an index error, try printing your list or the length of your list to see if it is different than you expected.
More detailsPublished - Tue, 11 Apr 2023
Sat, 15 Apr 2023
Sat, 15 Apr 2023
Sat, 15 Apr 2023
Write a public review