List Flashcards
returns the length of a list
len(my_collection)
Add multiple items to a list
my_collection.extend([ More”, “Items”])
Add a single item to a list
my_collection.append(“Single”)
1.Delete the object of a list at a specified index
2. Delete item by value don’t return
3.Delete and returns an item at a given index
- del my_list[2]
- my_list.remove(“apple”)
- my_list.pop(“apple”)
Clone a list 3 ways
clone = my_collection[:]
clone = list(original_list)
clone = original_list.copy()
Concatenate two lists
my_collection_2 = [“a”, “b”, “c”]
my_collection_3 = my_collection + my_collection_2
Calculate the sum of a list of ints or floats
number_collection = [1,2,3,4.5]
sum(number_collection)
Check if an item is in a list, returns Boolean
Check if an item is not in a list, returns Boolean
item in my_collection
item not in my_collection
Removes all the elements from the list
list.clear()
Returns the index of the first element with the specified value
my_list = [3,5,6,5,7]
my_list.index(5) returns 1, the index of the first occurrence of 5.
Returns the number of elements with the specified value.
my_list = [3,5,6,5,7,5]
my_list.count(5) returns 3
Sorts the list and reverse it
list.sort().reverse()
new_list = sorted(example_list) returns a new sorted list
Min Max and Sum
e_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
min(e_list) returns 1
max(e_list) returns 9
sum(e_list) returns 44, the sum of the items in the list.
Returns True if all elements of the list are true (or if the list is empty)
Returns True if any element of the list is true. If the list is empty, returns False.
all([True, True, False]) returns False, as not all elements are True.
any([True, False, False]) returns True, as at least one element is True.
Converts an iterable (tuple, string, set, dictionary) to a list.
tuple_k = (4,5,6)
my_list = list(tuple_k)