Why to use List Comprehension in Python | List Comprehension in Python | AskTheCode
Python is a dynamically typed, one of the most popular programming languages. Its dynamicity provides Python programmers a list of unique features and modules, that make programming easy for them.
One of the preferable special features of Python that is very much popular among the pro coders is List Comprehension, this interesting python feature enables you to solve most of the problems in a single line.
What is List Comprehension in Python?
Before heading towards its use, let’s know first, what this “List Comprehension” is. In Python, list comprehension is a syntactic construct that helps a programmer to create a new list based on the existing list. Now, let’s understand this using an example: Consider a list of numbers, and you have to add 2 to the elements present at the even indices and 1 at the odd indices in the list. Normally, a beginner approaches/solves such problem using a for loop like demonstrated below: Solution using a for-loop:
def addOneAndTwo(list, n):
for i in range(n):
if i % 2 == 0:
list[i] += 2
else:
list[i] += 1
return list
The same problem when given to intermediate or advanced level programmers, they solve such problems using list comprehension. Let’s see the approach of solving the above problem using list comprehension.
Solution using list comprehension:
def addOneAndTwo(nums, n):
return [nums[i] + 1 if i % 2 == 1 else nums[i] + 2 for i in ange(n)]
From this discussion, you can easily analyze and understand the effectiveness of list comprehension in Python that how it simplifies the code from 6 lines to 1 line.
Let’s see the use of list comprehension with some more problems:
Let’s again experience the power of the most interesting feature in python with some popular problems asked in popular MNCs.
Shuffle The Array Given an array Array having 2n elements in the form [x1, x2, x3, …, xn, y1, y2, …, yn]. Return the array in the shuffled form as [x1, y1, x2, y2, …, xn, yn]. Example: Input: Array = {2, 5, 1, 3, 4, 7}, n = 3 Output: [2, 3, 5, 4, 1, 7] Explanation: Since x1 = 2, x2 = 5, x3 = 1, y1 = 3, y2 = 4, y3 = 7. Then, the answer is [2, 3, 5, 4, 1, 7]. Solution:
def shuffle(self, nums, n):
return reduce(lambda a, b: a + b, [[nums[i], nums[j]] for i, j in zip(range(0, n), range(n, 2 * n))])
Richest Customer’s Wealth You are given an M x N integer grid accounts where accounts[ i ][ j ] is the amount of money the i th customer has in the j th bank. Return the wealth that the richest customer has. A customer’s wealth is the amount of money they have in all their bank accounts. And, the richest customer is the customer having the maximum wealth. Example: Input: accounts = [ [1, 2, 3], [3, 2, 1] ]. Output: 6 Explanation: 1st customer has a wealth = 1 + 2 + 3 = 6. And, the 2nd customer has wealth = 3 + 2 + 1 = 6. Both the customers are considered as richest, as they have the same wealth, so return the maximum wealth, i.e., 6. Solution:
def maximumWealth(self, accounts):
return max([sum(row) for row in accounts])
Decompress Run Length Encoded List You are given a list of integer list which represent the list as compressed with run-length encoding. Consider each adjacent pair of elements [freq, val] = [list [2*i], list [2*i+1] ], where i >= 0. For each such pair, there are freq elements with value val concatenated in a sub-list. Concatenate all the sub-lists from left to right to generate the decompressed list. And, then return the decompressed list. Example: Input: list = [ 1, 2, 3, 4 ]. Output: [ 2, 4, 4, 4 ] Explanation: In the first pair [1, 2], freq = 1 and val = 2, so generate the array as [2]. In the second pair [3, 4], freq = 3 and val = 4. So generate the array as [4, 4, 4]. At the end, concatenate [2] + [4, 4, 4], this results in [2, 4, 4, 4]. Solution:
def decompress_R_L_E_list(self, nums):
return reduce(lambda a, b: a + b, [[nums[i + 1]] * nums[i] for i in range(0, len(nums), 2)])
Commentaires