Python Lists Flashcards
Methods for Lists
Append
Remove
List Method: Append
list.append(Object my_value) - Adds object to the end of the list
List Method: Remove
list.remove(Object my_value) - Removes the given object from the list
In a list with multiple instances of a value, only the first instance will be removed.
List Method: + (Concatenate)
list3 = list1 + list2 or list3 = list1 + [Object value1, Object value2]
append list2 to list1
List Method: [ ] (Square Brackets)
my_list[int index] - Retrieve the object that is referenced by the zero-indexed value
In Python, we can negatively index any element starting at -1 for the last element of the list, -2 for the second-to-last element and so on.
Multi-Dimensional List
Useful for representing grids
List Method: Count
my_list.count(Obj my_value) - Count the number of occurrences of an element in a list
return: int
time complexity: O(n)
List Method: Insert
my_list(int Index, Obj my_value) - Insert an element (my_value) into a specific index of a list.
returns: void
Time Complexity: O(n) unless element is added to end, and then O(1)
List Method: Pop
my_list.pop(int index = -1) - Remove an element from a specific index or from the end of a list. The index value is optional.
returns: popped element
Time Complexity: O(n) unless element is removed from the back, and then O(1)
List Method: Range (Python specific)
A built in Python function to create a sequence of integers
List Method: Len (Python specific)
A built in Python function to get the length of a list
List Method: Sort
my_list.sort() - Sort a list
To sort in reverse order -> my_list.sort(reverse=True)
return: void
time complexity: O(n log(n))