🤐 Zip Function
Zip function pairs items by index from iterables into tuples.
returns a Zip object
Items without a pair are not carried over.
Great for taking 2 iterables and making a dictionary
A function declared and called in one line
- can take any number of arguments but only executes one expression, which may or may not return a value
- can be used to return function objects
- frequently used when another function takes a function as a parameter
add1 = lambda x, y: x + y # is equivalent to writing... def add2(x, y): return x + y
Sorting using lambdas
sorted() take a key parameter function to compare items while sorting. This is a great place to use lambdas
>>>sorted("the cat is brown".split(), key=lambda s:s[-1]) >>>['the', 'brown', 'is', 'cat']
if else statements can run on one line as well!
This is the same as a ternary operator in Java
valid = True if num > 99 else False
An unordered group of non-duplicate values
Unique mutable sequence of immutable objects
Supports casting but NOT indexing or slicing
# remove duplicates in a list of cities >>>how_many = lambda cities: list(set(cities)) >>>how_many(["Atlanta", "Seattle", "Seattle"]) >>>["Atlanta", "Seattle"]
Methods and Operators