Python Lambda
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
Syntax
lambda arguments : expression
The expression is executed and the result is returned:
ExampleGet your own Python Server
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
Lambda functions can take any number of arguments:
Example
Multiply argument a with argument b and return the result:
x = lambda a, b : a * b
print(x(5, 6))
Example
Summarize argument a, b, and c and return the result:
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
The power of lambda is better shown when you use them as an anonymous function inside
another function.
Say you have a function definition that takes one argument, and that argument will be multiplied
with an unknown number:
Examp1
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
Output:22
Exampl2:
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
Output:22
33
Array used as List
cars = ["Ford", "Volvo", "BMW"]
for x in cars:
print(x)
Output:-
Ford
Volvo
BMW
Adding Array Elements
You can use the append() method to add an element to an array.
cars = ["Ford", "Volvo", "BMW"]
cars.append("Honda")
print(cars)
Output:-
['Ford', 'Volvo', 'BMW', 'Honda']
Removing Array Elements
You can use the pop() method to remove an element from the array.
cars = ["Ford", "Volvo", "BMW"]
cars.pop(1)
print(cars)
Output:-['Ford', 'BMW']
You can also use the remove() method to remove an element from the array.
cars = ["Ford", "Volvo", "BMW"]
cars.remove("Volvo")
print(cars)
Output:-
['Ford', 'BMW']
Note: The list's remove() method only removes the first occurrence of the specified value.
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count()Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
['apple', 'banana', 'cherry', 'orange']
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)
Output-[]
fruits = ['apple', 'banana', 'cherry', 'orange']
x = fruits.copy()
Output:-
['apple', 'banana', 'cherry']
fruits = ["apple", "banana", "cherry"]
x = fruits.count("cherry")
print(x)
Output:-1
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)
Output-
['apple', 'banana', 'cherry', 'Ford', 'BMW']
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)
Output
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
Output
['apple', 'orange', 'banana', 'cherry']
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
Output-
['apple', 'cherry']
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)
Output-
['apple', 'cherry']
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
Output-
['cherry', 'banana', 'apple']
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
Output-
['BMW', 'Ford', 'Volvo']