0% found this document useful (0 votes)
58 views3 pages

5 Powerful Python One-Liners You Should Know: 1-Typecasting of All Items in A List

This document presents 5 useful Python one-liners: 1) Using map() to typecast all items in a list to the same type like int or float. 2) Using lambda, map(), and str() to sum the digits of an integer. 3) Using list comprehension to flatten a nested list into a single list. 4) Using zip() to transpose a matrix by switching rows and columns. 5) Using dictionary comprehension to swap dictionary keys and values.

Uploaded by

Maithili Karande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views3 pages

5 Powerful Python One-Liners You Should Know: 1-Typecasting of All Items in A List

This document presents 5 useful Python one-liners: 1) Using map() to typecast all items in a list to the same type like int or float. 2) Using lambda, map(), and str() to sum the digits of an integer. 3) Using list comprehension to flatten a nested list into a single list. 4) Using zip() to transpose a matrix by switching rows and columns. 5) Using dictionary comprehension to swap dictionary keys and values.

Uploaded by

Maithili Karande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5 Powerful Python

One-Liners You Should

Know
1- Typecasting of all items in a list
This is useful when you need to change the type of values in a list.

Example 1:
>>> list(map(int, ['1', '2', '3']))
Output:
[1, 2, 3]

Example 2:
This is also useful when we want to make a list consisting of heterogonous
items homogeneous.
>>> list(map(float, ['1', 2, '3.0', 4.0, '5', 6]))
Output:
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

2- Sum of digits of an integer

You don’t have to use mathematical operators, i.e. / (division) and %


(modulo) operators, to compute the sum of digits of an integer number.
There is an alternative way to do that. If we convert the number to a string,
everything becomes easy. But how? We’ve already known that strings are
iterable so we can iterate over characters of a string with map() function.

We’ll basically use this logic.


>>> sum_of_digits = lambda x: sum(map(int, str(x)))
Example:
>>> print(sum_of_digits(1789))
Output:
25

3- Flat a list that contains sublists

Suppose that we have a list like that:


>>> l = [[1, 2, 3], [4, 5], [6], [7, 8], [9]]

And we want to flat the list so that the result becomes:


[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> flattened_list = [item for sublist in l for item in sublist]

Example:
>>> l = [[1, 2, 3], [4, 5], [6], [7, 8], [9]]
>>> flattened_list = [item for sublist in l for item in sublist]
>>> print(flattened_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

4- Transpose of a Matrix

This is a good practice to use zip() function.


>>> transpose_A = [list(i) for i in zip(*A)]
Example:
>>> A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> transpose_A = [list(i) for i in zip(*A)]

Let’s use NumPy to print the 2d array:


import numpy as np

A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(np.matrix(A))

transpose_A = [list(i) for i in zip(*A)]

print()

print(np.matrix(transpose_A))
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 4 7]
[2 5 8]
[3 6 9]]

5- Swap keys and values in a dictionary

Suppose that we have staff data storing in a dict and want to exchange
key-value pairs.
>>> staff = {'Data Scientist': 'John', 'Django Developer': 'Jane'}
We can use dictionary comprehension to swap key-value pairs.
>>> staff = {i:j for j, i in staff.items()}
Let’s print the result:
>>> print(staff)
Output:
{'John': 'Data Scientist', 'Jane': 'Django Developer'}

-------------------------------------------------------------------------------------------------------------------------------

Source: https://levelup.gitconnected.com/5-powerful-python-one-liners-you-should-know-469b9c4737c7

You might also like