Chapter 7: Lists
list = [item1, item2, etc.]
List: an object that contains multiple data items
list[start : end] #If start not specified, 0 is used for start index #If end not specified, len(list) is used for end index
Slice: a span of items that are taken from a sequence - List slicing format: list[start : end]
item in list #usualy in the context of a loop ie. for item in list:
You can use the in operator to determine whether an item is contained in a list (not in can also be used)
lst.append(item) #where lst is a list
append(item): used to add items to a list - item is appended to the end of the existing list
del list[i]
del statement: removes an element from a specific index in a list
list1 = [1, 2, 3, 4] list2 = [item for item in list1]
how to copy a list
lst.index(item) #where lst is a list
index(item): used to determine where an item is located in a list
lst.insert(index, item) #where lst is a list
insert(index, item): used to insert item at position index in the list
size = len(my_list)
len function: returns the length of a list as an integer
list(str) #where str is a string
list() function can convert certain types of objects to lists
max(lst) or min(lst) #where lst is a list
min and max functions: built-in functions that returns the item that has the lowest or highest value in a sequence
print(lst) #where lst is a list
print function can be used to display an entire list
lst.remove(item) #where lst is a list
remove(item): removes the first occurrence of item in the list
lst.reverse() #where lst is a list
reverse(): reverses the order of the elements in the list
lst.sort() #where lst is a list
sort(): used to sort the elements of the list in ascending order
list[1] = new_value
• list[1] = new_value can be used to assign a new value to a list element