Python Lists
Lists are used to store multiple items in one variable. They are one of the four data abstractions, meaning they are used to college data. They are defined using square brackets and have values inside seperated by commas.
List items are ordered, changeable, and allow for duplicate values. Lists are 0 indexed so the first item has an index of 1, second item has a index of 1, third has an index of 2 and so forth. The last item also has an additional index of -1. You can use the format list_name[index] to find the list value at that particular index. List items can be of any data type, string, integer, float, boolean, etc.
Lists are ordered bmeaning the items have a defined order in which they are organized and that order will not change. New items are always placed at the end of the list unless they are inserted at a particular index in which case they do change the order of the list. Lists are also changeable meaning we can add, remove, and replace items in the list after it has been created. Lists also allow for duplicates so we can have multiple items of the same value.
To find the length of a list, use the len() keyword and pass in the list name as the parameter.
You can convert other data abstractions like tuples, dictionaries, and sets into lists by using the list() function and passing in the data abstraction as a parameter.
To change a list item, find the index of the item you want to change and do list_name[index] = new_value to change the value at that index. You can also change a range of item values by doing list_name[first_index:last_index] = [new_value, new_value..] where the first index corresponds to the first value you want to change and the last index is for the last value you want to replace.
To add new values to a list, use .append() in the format list_name.append(new_value).
Use
.extend() if you want to add multiple new values or if you want to combine the values in two data types like list and
tuples.
Use .insert() in the format this_list.insert(index, new_value) if
you don't want to add the value to the end of the list and at a particular index instead.
Use list_name.pop(index) to remove items
by index or .remove(item_name) to remove items by value.