Practice Flashcards
Reverse list
.reverse() in place, only for list
reversed() returns iterator
(if the input list changes iterator sees it)
[::-1] reversed copy, slow
Sequence Functions
len()
min()
max()
Functions that return iterator
reversed(), enumerate(), zip(), map(), filter()
Generator Usage
infinite sequence
Map
map(function, iterables)
transformation function
lambda function (don’t use!)
Filter
map(function, iterable)
map(None, iterable) True elements
lambda function (don’t use!)
Generator expression without ()
list(square(x) for x in numbers)
Sum
sum(iterable, start)
start = 0
can’t sum strings
Zip
zip(*iterables, strict=False)
strict=True raises ValueError if iterables have different length
dict(zip(keys, values))
Enumerate
enumerate(iterable, start=0)
Dictionary update()
dict.update(iterable or kwargs)
iterable: dict, iterable of tuples etc.
Sort
sort(*, key=None, reverse=False)
list.sort()
Max, Min
max(iterable, *, key=None)
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)
All, Any
all(iterable)
True if iterable is empty
any(iterable)
False if iterable is empty