Software engineering and personal development

Month: May 2021

How to Remove Duplicate Elements in a List in Python

MacBook Pro, white ceramic mug,and black smartphone on table
Photo by Andrew Neel on Unsplash

Lists are part of typically every project that you work on these days. You have lists of people, a list of products, a list of activities, and the list of lists goes on.

There can be times when you want to only have unique elements in that list, for example, if someone has liked multiple posts and you want to get all the people who have liked one of your posts. You do not want to include that same person multiple times in that list.

Continue reading

How to Check Whether 2 Strings Are Anagrams in Python Using Counter

A MacBook with lines of code on its screen on a busy desk
Photo by Christopher Gower on Unsplash

Checking whether a word is an anagram of another one can be a question that is quite common in coding interviews.

If you don’t know what an anagram is, here is a definition:

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy and the word adobe into abode. 

Continue reading

10 Quick Python Snippets That Can Help You Work with Dates

Photo by Brad Neathery on Unsplash

If you use Python in your work, or simply for scripting, chances are that there will come a point in time when you are going to need to work with dates.

It can be doing a conversion from one format to the other one, changing the timezone, etc.

Since you have already decided to read this article, then why don’t we stop wasting time talking and just start immediately learning?

1. Convert a string into a date

Despite being engineers, we still spend a considerable amount of time using a natural language to talk.

Since that is the case, we can also convert a string looking like a natural phrase into a date as shown below:

In this example:

  • %d — represents the day of the month, like 21, 22, 23, etc.
  • %B — represents the actual name of the month like January, February, March etc.
  • %Y — represents the year, like 2020, 2021, etc.

2. Convert from one timezone to another one

As we are mostly working remotely these days, we may have the chance to also work with someone who is in another timezone.

You can of course just Google and find out what time it is now in New York City even if you have never been before, but there is joy in implementing this yourself.

Let’s see how to do that.

3. Find the number of days between 2 dates

Since we still live in this universe where time passes, we may need to find the number of days that have passed between one point and another one.

We can of course pull up a calendar and manually count the days, which is slow and boring.

To do that quickly, we can import the date module in our Python script and initially prepare the dates:

After that, we can basically just find the difference between 2 numbers:

Yes, it’s that simple.

You do not have to spend hours doing it manually on a physical calendar. It can take barely a few moments to do it like this.

4. Add days to a date

Let us imagine that you are working on a feature of a product in which the subscription for a user expires 90 days from now. You want to save up that date in the database using the following:

5. Add weeks to a date

You can also just change that to weeks, like the following:

6. Subtract days from a date

Have you ever wanted to go back in time and improve something in your life, stayed more with your loved ones, or simply start investing in Bitcoin a little bit earlier?

Well, I wish I could tell you how to do that.

However, if you want to find a date in the past, let’s say 180 days from now, you can easily do that using the following:

7. Subtract weeks from a date

As you may have guessed already, you can also find a date by subtracting weeks:

8. Find the difference between 2 dates in months

We can also find the difference between the 2 dates in months quite quickly.

We should keep in mind that we may have dates that are in different years, and it’s not enough to think that we can just subtract two months.

For example, between October 2018 and November 2021, there isn’t just a month difference. There is also a difference in years as well.

9. Put days before months

The majority of the world puts days in front of months in their dates:

Image: John Harding/Mona Chalabi Photograph: John Harding/Mona Chalabi

However, there can be cases when you get as input a date where the month is in front of the day.

If you want to convert those dates in the format that the majority of the world uses, here is how:

10. Find the first day of the month

In some companies, they pay their employees on the first day of every month.

If you want to know what day of the week will that be, then this snippet can be helpful:


That’s pretty much it. I hope you learned at least something in this article.

© 2024 Fatos Morina

Theme by Anders NorenUp ↑