I think to get started with Python, we have plenty of resources available on web. I would like to add some topics which are useful and would help reducing some line of codes for sure. Also, your code would look more crisp with implementation of these concepts. I like using these while wrangling with data, hence would not mention python concepts.
Lambda
Also known as Anonymous functions, Lambda is an alternative to a function which we can use when we want to give only one expression. Also, we may or may not assign this lambda to any keyword as per our use.
#Add function to add two number
def add(x, y):
… return x + y
#Lambda for adding two numbers
add = lambda x, y: x + y
#Using the lambda
>>> (lambda x, y: x + y)(4, 5)
9
This Lambda is very useful when we need to pass these in the map, filter and list comprehensions which are very useful when working with data. Now lets go ahead with Map, Filter and List Comprehensions, so that we can see the implementation of Lambda.
Maps
Map functions are utilized when we want to apply a function/lambda to an iterable element like lists, tuples, sets.
map(func, iter)
# Adding elements of two lists
a = [1,2,3,4]
b = [9,8,7,6]
#Passing a lambda in map as a function to be applied to the lists
result = map(lambda x,y:x+y, a, b)
print(list(result))
>>>[10,10,10,10]
#Let’s see two functions which can be used to convert from Fahrenheit to Celcius or vice-versa
def fahrenheit(T):
return ((int(9)/5)*T + 32)
def celsius(T):
return (int(5)/9)*(T-32)
temp = (20,30,40,50)
F = map(fahrenheit, temp)
C = map(celsius, F)
#Now looking at the logic, we can convert these functions into lambdas
>>>Celsius=[20,30,40,50]
>>>Fahrenheit = map(lambda x: (int(9)/5)*x + 32, Celsius)
>>>print(list(Fahrenheit))
[68.0, 86.0, 104.0, 122.0]
>>>C = map(lambda x: (int(5)/9)*(x-32), Fahrenheit)
>>>print(list(C))
[20.0, 30.0, 40.0, 50.0]
>>>
Filter
Filter creates a list of elements where the function returns true.
filter(func, iter)
# Adding elements of two lists
>>>a = [-1,-2,-3, 0, 5, 0, 7]
#Passing a lambda in map as a function to be applied to the lists
>>>result = filter(lambda x : x==0, a)
>>>print(list(result))
[0 , 0]
#Another use case, if we want to find out the intersection between two lists, we can do that using the filter.
>>>a = [1,2,3,5,7,9]
>>>b = [2,3,5,6,7,8]
>>>print(list(filter(lambda x: x in a, b)))
[2, 3, ,5 ,7]
List Comprehension
List comprehension is a concise and a quick way to create a list using a logic and other lists if required. Its syntax works bit differently for different requirement.
[ expression for item in list if conditional ]
So, ‘if’ goes at last when used in list comprehension, whereas if you need to use ‘if else’ the syntax changes
[ expression if conditional else statement for item in list ]
#easiest example to start with
>>>[x.lower() for x in [“A”,”B”,”C”]]
[‘a’, ‘b’, ‘c’]
#lets try something better
>>>listOfWords = [“this”,”is”,”a”,”list”,”of”,”words”]
>>>items = [ word[0] for word in listOfWords ]
>>>print items
[‘t’, ‘i’, ‘a’, ‘l’, ‘o’, ‘w’]
#Lets try the if condition now
>>>aString = “Hey there 12345 How are you ? 6789”
>>>numbersList = [int(x) for x in aString if x.isdigit()]
>>>print(numbersList)
[1,2,3,4,5,6,7,8,9]
#Lets try if else condition now
>>>result = [“Even” if i%2==0 else “Odd” for i in range(10)]
>>>print(result)
[‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’, ‘Even’, ‘Odd’]
# One more quick example of lambda with list comprehension
#To square the numbers
>>> x = [(lambda x: x*x)(x) for x in range(10)]
>>>print(x)
#Though there is easy way to do that, but this is just an example for you to try this out as per your need.
>>> x = [x*x for x in range(10)]
>>>print(x)