Categories
Python Answers

How to create a GUID or UUID in Python?

Sometimes, we want to create a GUID or UUID in Python.

In this article, we’ll look at how to create a GUID or UUID in Python.

How to create a GUID or UUID in Python?

To create a GUID or UUID in Python, we can use the uuid module.

For instance, we write:

import uuid

id = uuid.uuid4()
print(id)

We call uuid.uuid4 to return a v4 UUID as a string.

Therefore, id is something like 'c55b58d6-aa0d-498e-96be-433f99492cfb'.

Conclusion

To create a GUID or UUID in Python, we can use the uuid module.

Categories
Python Answers

How to delete a character from a string using Python?

Sometimes, we want to delete a character from a string using Python.

In this article, we’ll look at how to delete a character from a string using Python.

How to delete a character from a string using Python?

To delete a character from a string using Python, we can use the string’s replace method.

For instance, we write:

oldstr = 'EXAMPLE'
newstr = oldstr.replace("M", "")
print(newstr)

We call oldstr.replace with 'M' and an empty string to remove ‘M’ from 'EXAMPLE' and return the new string

Therefore, newstr is 'EXAPLE'.

Conclusion

To delete a character from a string using Python, we can use the string’s replace method.

Categories
Python Answers

How to display a float with two decimal places with Python?

Sometimes, we want to display a float with two decimal places with Python.

In this article, we’ll look at how to display a float with two decimal places with Python.

How to display a float with two decimal places with Python?

To display a float with two decimal places with Python, we can use the string’s format method.

For instance, we write:

n = "{:.2f}".format(5)
print(n)

We call format on "{:.2f}" with a number to render the number 5 with 2 decimal places.

Therefore, n is 5.00.

Conclusion

To display a float with two decimal places with Python, we can use the string’s format method.

Categories
Python Answers

How to get permutations between two lists of unequal length with Python?

Sometimes, we want to to get permutations between two lists of unequal length with Python.

In this article, we’ll look at how to get permutations between two lists of unequal length with Python.

How to get permutations between two lists of unequal length with Python?

To get permutations between two lists of unequal length with Python, we can use the itertools.product method.

For instance, we write:

import itertools
from pprint import pprint

inputdata = [
    ['a', 'b', 'c'],
    ['d'],
    ['e', 'f'],
]
result = list(itertools.product(*inputdata))
pprint(result)

We call itertools.product with the lists unpacked from inputdata.

Then we call list on the returned iterable to convert it to a list and assign the returned list to result.

Therefore, result is:

[('a', 'd', 'e'),
 ('a', 'd', 'f'),
 ('b', 'd', 'e'),
 ('b', 'd', 'f'),
 ('c', 'd', 'e'),
 ('c', 'd', 'f')]

Conclusion

To get permutations between two lists of unequal length with Python, we can use the itertools.product method.

Categories
Python Answers

How to split a list into N parts of approximately equal length with Python?

Sometimes, we want to split a list into N parts of approximately equal length with Python.

In this article, we’ll look at how to split a list into N parts of approximately equal length with Python.

How to split a list into N parts of approximately equal length with Python?

To split a list into N parts of approximately equal length with Python, we can use list comprehension.

For instance, we write:

def chunkify(lst, n):
    return [lst[i::n] for i in range(n)]


chunks = chunkify(list(range(13)), 3)
print(chunks)

We define the chunkify function to split the lst list into n chunks.

To do this, we use list comprehension to return slices of list with from index i to the end with n items in each chunk.

Then we call chunkify with the list(range(13)) list and 3 to divide the list into 3 chunks.

Therefore, chunks is [[0, 3, 6, 9, 12], [1, 4, 7, 10], [2, 5, 8, 11]].

Conclusion

To split a list into N parts of approximately equal length with Python, we can use list comprehension.