Categories
Python Answers

How to create an empty list in Python with certain size?

To create an empty list in Python with certain size, we can create a list with all None with the given size.

For instance, we write

l = [None] * 10

to create list l of size 10 that has all None‘s inside.

Categories
Python Answers

How to convert a nested Python dict to an object?

Sometimes, we want to convert a nested Python dict to an object.

In this article, we’ll look at how to convert a nested Python dict to an object.

How to convert a nested Python dict to an object?

To convert a nested Python dict to an object, we can use the bunch library.

To install it, we run

pip install bunch

Then we use it by writing

from bunch import bunchify

d = {"a": 1, "b": {"c": 2}, "d": ["hi", {"foo": "bar"}]}
x = bunchify(d)

to call bunchify with dict d to return the dict’s content on an object.

Then we can use x.a to access the value of d with key 'a'.

And we can use x.b.c to access the entry with key 'c' in the dict d with key 'b'.

Conclusion

To convert a nested Python dict to an object, we can use the bunch library.

Categories
Python Answers

How to combine two lists in an alternating fashion with Python?

Sometimes, we want to combine two lists in an alternating fashion with Python

In this article, we’ll look at how to combine two lists in an alternating fashion with Python.

How to combine two lists in an alternating fashion with Python?

To combine two lists in an alternating fashion with Python, we can use the slice syntax.

For instance, we write

list1 = ['f', 'o', 'o']
list2 = ['hello', 'world']
result = [None] * (len(list1) + len(list2))
result[::2] = list1
result[1::2] = list2

to create the result list that has the length of list1 and list2 combined with

result = [None] * (len(list1) + len(list2))

Then we use

result[::2] = list1

to put the items in list1 in the even indexes of result.

And we use

result[1::2] = list2

to put the items in list1 in the odd indexes of result.

Conclusion

To combine two lists in an alternating fashion with Python, we can use the slice syntax.

Categories
Python Answers

How to calculate the time interval between two time strings with Python?

Sometimes, we want to calculate the time interval between two time strings with Python.

In this article, we’ll look at how to calculate the time interval between two time strings with Python.

How to calculate the time interval between two time strings with Python?

To calculate the time interval between two time strings with Python, we can convert the time strings to datetimes with strptime and then take their difference.

For instance, we write

from datetime import datetime

s1 = '10:33:26'
s2 = '11:15:49' 
format = '%H:%M:%S'
tdelta = datetime.strptime(s2, format) - datetime.strptime(s1, format)

to call strptime with the time strings s2 and s1 and specify that they’re in the format format to convert the strings to datetime objects.

Then we subtract them to get a timedelta object with the difference between them.

Conclusion

To calculate the time interval between two time strings with Python, we can convert the time strings to datetimes with strptime and then take their difference.

Categories
Python Answers

How to check for palindrome using Python?

Sometimes, we want to check for palindrome using Python.

In this article, we’ll look at how to check for palindrome using Python.

How to check for palindrome using Python?

To check for palindrome using Python, we can check if the reversed version of the string is the same as the original string.

To do this, we write

is_palindrome = str(n) == str(n)[::-1]

to invert the string n with str(n)[::-1].

And then we check that the inverted string is the same as n with ==.

Conclusion

To check for palindrome using Python, we can check if the reversed version of the string is the same as the original string.