Categories
Python Answers

How to get a list of values from a list of dicts with Python?

Sometimes, we want to get a list of values from a list of dicts with Python.

In this article, we’ll look at how to get a list of values from a list of dicts with Python.

How to get a list of values from a list of dicts with Python?

To get a list of values from a list of dicts with Python, we can use list comprehension.

For instance, we write:

dicts = [{
    'value': 'apple',
    'blah': 2
}, {
    'value': 'banana',
    'blah': 3
}, {
    'value': 'cars',
    'blah': 4
}]

values = [d['value'] for d in dicts if 'value' in d]
print(values)

We get the entries in dicts with for d in dicts.

Then we get the value of each entry d with d['value'].

And we only return the entries that has the value key with if 'value' in d.

Therefore, values is ['apple', 'banana', 'cars'].

Conclusion

To get a list of values from a list of dicts with Python, we can use list comprehension.

Categories
Python Answers

How to find the MIME type of a file in Python?

Sometimes, we want to find the MIME type of a file in Python.

In this article, we’ll look at how to find the MIME type of a file in Python.

How to find the MIME type of a file in Python?

To find the MIME type of a file in Python, we can use the python-magic package.

To install it, we run:

pip install python-magic

Then we use it by writing:

import magic

mime = magic.Magic(mime=True)
t = mime.from_file("foo.csv")
print(t)

We invoke the Magic constructor with mime set to True to let us get the MIME type of the file.

Then we call mime.from_file with the path of the file to get the MIME type of to return the MIME type of the file as a string.

Therefore, t is something like 'text/plain'.

Conclusion

To find the MIME type of a file in Python, we can use the python-magic package.

Categories
Python Answers

How to remove duplicate dicts in list in Python?

Sometimes, we want to remove duplicate dicts in list in Python.

In this article, we’ll look at how to remove duplicate dicts in list in Python.

How to remove duplicate dicts in list in Python?

To remove duplicate dicts in list in Python, we can use list comprehension.

For instance, we write:

d = [{'a': 123}, {'b': 123}, {'a': 123}]
no_dups = [i for n, i in enumerate(d) if i not in d[n + 1:]]
print(no_dups)

We have the d list with duplicate dicts.

Then we use not in d[n + 1:] to filter out the dicts that are duplicates of the dict in index i.

And then we assign the resulting list to no_dups.

Therefore, no_dups is:

[{'b': 123}, {'a': 123}]

Conclusion

To remove duplicate dicts in list in Python, we can use list comprehension.

Categories
Python Answers

How to make a timezone aware datetime object in Python?

Sometimes, we want to make a time zone aware datetime object in Python.

In this article, we’ll look at how to make a time zone aware datetime object in Python.

How to make a timezone aware datetime object in Python?

To make a time zone aware datetime object in Python, we can use the pytz module.

For instance, we write:

import datetime
import pytz

unaware = datetime.datetime(2020, 8, 15, 8, 15, 12, 0)
aware = datetime.datetime(2020, 8, 15, 8, 15, 12, 0, pytz.UTC)

now_aware = pytz.utc.localize(unaware)
assert aware == now_aware

We create datetime objects with the datetime.datetime method.

And as pass in the time zone as the last argument of datetime.datetime to create a UTC datetime.

Without the time zone argument, then datetime object isn’t time zone aware.

To convert a time zone unaware datetime to a time zone aware datetime, we call pytz.utc.localize.

Therefore, aware and non_aware are the same since we made both datetimes time zone aware, have the same time zone, and have the same date and time.

Conclusion

To make a time zone aware datetime object in Python, we can use the pytz module.

Categories
Python Answers

How to format a string in Python?

Sometimes, we want to format a string in Python.

In this article, we’ll look at how to format a string in Python.

How to format a string in Python?

To format a string in Python, we can use the string’s format method.

For instance, we write:

s = "{0}, {1}, {2}".format(1, 2, 3)
print(s)

We call format with the string placeholders separated by commas.

We create placeholders by using integers surrounded by curly braces.

Therefore, s is '1, 2, 3'.

Conclusion

To format a string in Python, we can use the string’s format method.