# create a list
# length (number of elements)
# Is 'B' member in my list?
# find position of element in list
# remove element from list
# in case of multiple elements, 'remove' deletes only the first item
['A', 'C', 'B']
# remove multiple elements from multiple positions in a list
# remove letters 'B' and 'C' from all positions in a list ( → list comprehension)
mylist_reduced = [i for i in mylist if i not in remove_set] # create new reduced list
['A', 'D', 'A', 'D']
add (concatenate) lists
['A', 'B', 'C', 'D', 'E']
multi-copies of a list (repetition)
['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B']
['A', 'A', 'A', 'A', 'A']
>>> list2= ['B','C']
>>> [i for i in list1 if i not in list2] # keep list order
>>> list(set(list1) - set(list2)) #
indices (better avoid by using 'in', or a dictionary)
!! Python's 0-based indices start with 0 , not with 1
['Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct']
['Jan', 'Mar', 'May']
to access list elements by an index starting with 1, simply use a dummy first element 'None'
In short: 0-based is confusing when referring to objects: list-elements, items, fields, or boxes. But it make sense as a pointer (address) in time and data-streams (processing long files), referring to the start-position, not to the item, read more.
min / max
see more: → mean, median, ...
# get union, intersection, difference of lists:
# convert to list to set and use list operators
s1 & s2 # Intersection (overlap between both lists)
s1 | s2 # Union
set(['A', 'C', 'B', 'D'])
s1 ^ s2 # (symmetric) difference