Categories
Python Answers

How to sort list in descending order with Python?

Sometimes, we want to sort list in descending order with Python.

In this article, we’ll look at how to sort list in descending order with Python.

How to sort list in descending order with Python?

To sort list in descending order with Python, we can use the sorted function with the reverse parameter set to True.

For instance, we write:

timestamps = [
    "2020-04-20 10:07:30", "2020-04-20 10:07:38", "2020-04-20 10:07:52",
    "2020-04-20 10:08:22", "2020-04-20 10:08:22", "2020-04-20 10:09:46",
    "2020-04-20 10:10:37", "2020-04-20 10:10:58", "2020-04-20 10:11:50",
    "2020-04-20 10:12:13", "2020-04-20 10:12:13", "2020-04-20 10:25:38"
]
s = sorted(timestamps, reverse=True)
print(s)

We call sorted with the timestamps list and reverse set to True to sort timestamps in reverse order and return the sorted list.

Therefore, s is:

['2020-04-20 10:25:38', '2020-04-20 10:12:13', '2020-04-20 10:12:13', '2020-04-20 10:11:50', '2020-04-20 10:10:58', '2020-04-20 10:10:37', '2020-04-20 10:09:46', '2020-04-20 10:08:22', '2020-04-20 10:08:22', '2020-04-20 10:07:52', '2020-04-20 10:07:38', '2020-04-20 10:07:30']

Conclusion

To sort list in descending order with Python, we can use the sorted function with the reverse parameter set to True.

Categories
Python Answers

How to create a dictionary from a CSV file?

Sometimes, we want to create a dictionary from a CSV file.

In this article, we’ll look at how to create a dictionary from a CSV file.

How to create a dictionary from a CSV file?

To create a dictionary from a CSV file, we can use the csv.DictReader class.

For instance, we write:

import csv

reader = csv.DictReader(open("foo.csv"))
for row in reader:
    print(row)

to read the foo.csv file with open.

Then we use the returned file as the argument for csv.DictReader to convert the rows to dictionaries.

Next, we loop through the reader iterator with a for loop.

In the loop body, we print the row value, which are dictionaries of each row.

If foo.csv is:

foo,bar
1,2
3,4
5,6

Then the for loop prints:

{'foo': '1', 'bar': '2'}
{'foo': '3', 'bar': '4'}
{'foo': '5', 'bar': '6'}

Conclusion

To create a dictionary from a CSV file, we can use the csv.DictReader class.

Categories
Python Answers

How to convert a datetime object to milliseconds since epoch (Unix time) in Python?

Sometimes, we want to convert a datetime object to milliseconds since epoch (Unix time) in Python.

In this article, we’ll look at how to convert a datetime object to milliseconds since epoch (Unix time) in Python.

How to convert a datetime object to milliseconds since epoch (Unix time) in Python?

To convert a datetime object to milliseconds since epoch (Unix time) in Python, we can subtract the datetime from the epoch datetime.

And then we call total_seconds on the difference and multiply that by 1000.

For instance, we write:

import datetime

epoch = datetime.datetime.utcfromtimestamp(0)


def unix_time_millis(dt):
    return (dt - epoch).total_seconds() * 1000.0


dt = datetime.datetime(2020, 1, 1)
print(unix_time_millis(dt))

We use datetime.datetime.utcfromtimestamp(0) to create the Unix epoch datetime.

Then we define the unix_time_millis function that subtracts dt from epoch and call total_seconds on the difference.

And then we multiply that by 1000 to get the difference in milliseconds,

Next, we call unix_time_millis with dt to return the difference of dt since the Unix epoch in milliseconds.

Therefore, print should print 1577836800000.0.

Conclusion

To convert a datetime object to milliseconds since epoch (Unix time) in Python, we can subtract the datetime from the epoch datetime.

And then we call total_seconds on the difference and multiply that by 1000.

Categories
Python Answers

How to find the time difference between two datetime objects in Python?

Sometimes, we want to find the time difference between two datetime objects in Python.

In this article, we’ll look at how to find the time difference between two datetime objects in Python.

How to find the time difference between two datetime objects in Python?

To find the time difference between two datetime objects in Python, we can subtract 2 datetime objects directly.

For instance, we write;

import datetime

first_time = datetime.datetime(2021, 1, 1)
later_time = datetime.datetime(2021, 1, 5)
diff = later_time - first_time
print(diff)

We create a datetime objects with the datetime.datetime method.

Then we subtract later_time from first_time and assign that to diff.

Then from the print output, we see that diff is 4 days, 0:00:00.

Conclusion

To find the time difference between two datetime objects in Python, we can subtract 2 datetime objects directly.

Categories
Python Answers

How to delete a list element by value with Python?

Sometimes, we want to delete a list element by value with Python.

In this article, we’ll look at how to delete a list element by value with Python.

How to delete a list element by value with Python?

To delete a list element by value with Python, we can use the remove method.

For instance, we write:

a = ['a', 'b', 'c', 'd']
a.remove('b')
print(a)

We call a.remove to remove 'b' from a.

Therefore, a is now ['a', 'c', 'd'].

Conclusion

To delete a list element by value with Python, we can use the remove method.