Categories
Python Answers

How to get the difference between two dates in Python?

Sometimes, we want to get the difference between two dates in Python.

In this article, we’ll look at how to get the difference between two dates in Python.

How to get the difference between two dates in Python?

To get the difference between two dates in Python, we can subtract the 2 datetimes directly.

For instance, we write

from datetime import datetime

def days_between(d1, d2):
    d1 = datetime.strptime(d1, "%Y-%m-%d")
    d2 = datetime.strptime(d2, "%Y-%m-%d")
    return abs((d2 - d1).days)

to call strptime to convert the date strings d1 and d2 into datetime objects.

Then we subtract the 2 datetime objects with -.

Then we get the days difference between them with days.

And we return the absolute value of the difference with abs.

Conclusion

To get the difference between two dates in Python, we can subtract the 2 datetimes directly.

Categories
Python Answers

How to create a two way/reverse map with Python?

Sometimes, we want to create a two way/reverse map with Python.

In this article, we’ll look at how to create a two way/reverse map with Python.

How to create a two way/reverse map with Python?

To create a two way/reverse map with Python, we put the forward and reverse maps in the same dictionary.

For instance, we write

relation = {}
relation['Alice'] = 'Bob'
relation['Bob'] = 'Alice'

to create the relation dict.

Then we add both the mapping from 'Alice' to 'Bob' and vice versa in the same dictionary.

Conclusion

To create a two way/reverse map with Python, we put the forward and reverse maps in the same dictionary.

Categories
Python Answers

How to add type hint for a collection of a specified type with Python?

Sometimes, we want to add type hint for a collection of a specified type with Python.

In this article, we’ll look at how to add type hint for a collection of a specified type with Python.

How to add type hint for a collection of a specified type with Python?

To add type hint for a collection of a specified type with Python, we can specify the type after the colon.

For instance, we write

def my_func(l: list[int]):
    pass

to specify that the l parameter in my_func is a list of ints with list[int].

Conclusion

To add type hint for a collection of a specified type with Python, we can specify the type after the colon.

Categories
Python Answers

How to fix list index out of range error while iteratively popping elements with Python?

Sometimes, we want to fix list index out of range error while iteratively popping elements with Python.

In this article, we’ll look at how to fix list index out of range error while iteratively popping elements with Python.

How to fix list index out of range error while iteratively popping elements with Python?

To fix list index out of range error while iteratively popping elements with Python, we can use list comprehension.

For instance, we write

l = [x for x in l if x != 0]

to return a new list with the item x in list l that isn’t 0.

And then we assign the returned list back to l.

Conclusion

To fix list index out of range error while iteratively popping elements with Python, we can use list comprehension.

Categories
Python Answers

How to change backends in matplotlib and Python?

Sometimes, we want to change backends in matplotlib and Python.

In this article, we’ll look at how to change backends in matplotlib and Python.

How to change backends in matplotlib and Python?

To change backends in matplotlib and Python, we call use and then reload.

For instance, we write

import matplotlib

matplotlib.use('agg')

matplotlib = reload(matplotlib)
matplotlib.use('cairo.png')

to call matplotlib.use to set the back end.

And then we call reload with matplotlib and set the returned object back to matplotlib to reload the object.

And then we call use again to change the backend.

Conclusion

To change backends in matplotlib and Python, we call use and then reload.