Categories
Python Answers

How to use Python dict.fromkeys without all values pointing to same list?

Sometimes, we want to use Python dict.fromkeys without all values pointing to same list.

In this article, we’ll look at how to use Python dict.fromkeys without all values pointing to same list.

How to use Python dict.fromkeys without all values pointing to same list?

To use Python dict.fromkeys without all values pointing to same list, we can make a copy of the list with list and use dictionary comprehension.

For instance, we write:

keys = ['a', 'b', 'c']
value = [0, 0]
d = {key: list(value) for key in keys}
print(d)

We call list on value to return a copy of value for each entry.

And we use key in keys as the keys.

Therefore, d is {'a': [0, 0], 'b': [0, 0], 'c': [0, 0]} where [0, 0] in each entry is different.

Conclusion

To use Python dict.fromkeys without all values pointing to same list, we can make a copy of the list with list and use dictionary comprehension.

Categories
Python Answers

How to remove multiple spaces in a string with Python?

Sometimes, we want to remove multiple spaces in a string with Python.

In this article, we’ll look at how to remove multiple spaces in a string with Python.

How to remove multiple spaces in a string with Python?

To remove multiple spaces in a string with Python. we can use the re.sub method.

For instance, we write:

import re

s = "The   fox jumped   over    the log."
new_s = re.sub("\s\s+", " ", s)
print(new_s)

We call re.sub with the "\s\s+" regex string to look for 2 or more spaces.

And we replace them with a single space character.

s is the 3rd argument of sub so we’re doing the replacement on s and returning the new string.

We assign the returned string to new_s.

Therefore, new_s is 'The fox jumped over the log.'.

Conclusion

To remove multiple spaces in a string with Python. we can use the re.sub method.

Categories
Python Answers

How to parallelize a simple Python loop?

Sometimes, we want to parallelize a simple Python loop.

In this article, we’ll look at how to parallelize a simple Python loop.

How to parallelize a simple Python loop?

To parallelize a simple Python loop, we can use the joblib module.

For instance, we write:

from joblib import Parallel, delayed


def process(i):
    return i * i


results = Parallel(n_jobs=2)(delayed(process)(i) for i in range(10))
print(results)

We define our loop header with for i in range(10).

And in the loop body, we run delayed(process)(i).

Finally, we use the Parallel constructor with n_jobs set to 2 to use 2 CPU cores.

And then we assign the return results to results.

Therefore, results is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].

Conclusion

To parallelize a simple Python loop, we can use the joblib module.

Categories
Python Answers

How to convert binary to ASCII and vice versa with Python?

Sometimes, we want to convert binary to ASCII and vice versa with Python.

In this article, we’ll look at how to convert binary to ASCII and vice versa with Python.

How to convert binary to ASCII and vice versa with Python?

To convert binary to ASCII and vice versa with Python, we can use the binascii module.

To convert ASCII to binary, we write:

import binascii

b = bin(int(binascii.hexlify(b'hello'), 16))
print(b)

We call binascii.hexlify with b'hello' to convert the binary string to hex.

Then we call int to convert the hex to a decimal integer.

And then we call bin to convert the int to a binary string.

Therefore, b is 0b110100001100101011011000110110001101111.

To convert binary to ASCII, we call binascii.unhexlify.

For instance, we write:

import binascii

n = int('0b110100001100101011011000110110001101111', 2)
s = binascii.unhexlify('%x' % n)
print(s)

We call int with a binary string and 2 as the base.

Then we call binascii.unhexlify with '%x' % n to convert the int to a binary string.

Therefore s is b'hello'.

Conclusion

To convert binary to ASCII and vice versa with Python, we can use the binascii module.

Categories
Python Answers

How to make asynchronous requests with Python requests module?

Sometimes, we want to make asynchronous requests with Python.

In this article, we’ll look at how to make asynchronous requests with Python.

How to make asynchronous requests with Python?

To make asynchronous requests with Python, we can use the grequests module.

For instance, we write:

import grequests

urls = [
    'http://www.heroku.com',
    'http://tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://kennethreitz.com'
]

rs = (grequests.get(u) for u in urls)
r = grequests.map(rs)
print(r)

We call grequests.get with the entries in the urls list.

And then we assign the responses to rs.

Then we call grequests.map with rs to make all the requests at once and assign the list of responses to r.

Therefore, r is [<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>].

Conclusion

To make asynchronous requests with Python, we can use the grequests module.