Categories
Python Answers

How to search a list of dictionaries with Python?

Sometimes, we want to search a list of dictionaries with Python.

In this article, we’ll look at how to search a list of dictionaries with Python.

How to search a list of dictionaries with Python?

To search a list of dictionaries with Python, we can use list comprehension and the next function.

For instance, we write:

dicts = [{
    "name": "Tom",
    "age": 10
}, {
    "name": "Mark",
    "age": 5
}, {
    "name": "Pam",
    "age": 7
}]

d = next((item for item in dicts if item["name"] == "Pam"), None)
print(d)

to get get the dictionary with the name key set to 'Pam' with item for item in dicts if item["name"] == "Pam".

Then we get the first entry that matches with next.

We return None if there’re no matches.

Therefore, d is {'name': 'Pam', 'age': 7}.

Conclusion

To search a list of dictionaries with Python, we can use list comprehension and the next function.

Categories
Python Answers

How to add missing dates to Python Pandas DataFrame?

Sometimes, we want to add missing dates to Python Pandas DataFrame.

In this article, we’ll look at how to add missing dates to Python Pandas DataFrame.

How to add missing dates to Python Pandas DataFrame?

To add missing dates to Python Pandas DataFrame, we can use the DatetimeIndex instance’s reindex method.

For instance, we write:

import pandas as pd

idx = pd.date_range('09-01-2020', '09-30-2020')

s = pd.Series({
    '09-02-2020': 2,
    '09-03-2020': 1,
    '09-06-2020': 5,
    '09-07-2020': 1
})
s.index = pd.DatetimeIndex(s.index)

s = s.reindex(idx, fill_value=0)
print(s)

We create a date range index with idx = pd.date_range('09-01-2020', '09-30-2020').

Then we create a series with:

s = pd.Series({
    '09-02-2020': 2,
    '09-03-2020': 1,
    '09-06-2020': 5,
    '09-07-2020': 1
})

We set the index of the series with:

s.index = pd.DatetimeIndex(s.index)

Finally we fill in the missing dates between Sep 1, 2020 to Sep 30, 2020 with:

s = s.reindex(idx, fill_value=0)

Therefore, we see:

2020-09-01    0
2020-09-02    2
2020-09-03    1
2020-09-04    0
2020-09-05    0
2020-09-06    5
2020-09-07    1
2020-09-08    0
2020-09-09    0
2020-09-10    0
2020-09-11    0
2020-09-12    0
2020-09-13    0
2020-09-14    0
2020-09-15    0
2020-09-16    0
2020-09-17    0
2020-09-18    0
2020-09-19    0
2020-09-20    0
2020-09-21    0
2020-09-22    0
2020-09-23    0
2020-09-24    0
2020-09-25    0
2020-09-26    0
2020-09-27    0
2020-09-28    0
2020-09-29    0
2020-09-30    0
Freq: D, dtype: int64

printed.

Conclusion

To add missing dates to Python Pandas DataFrame, we can use the DatetimeIndex instance’s reindex method.

Categories
Python Answers

How to format a floating number to a fixed width in Python?

Sometimes, we want to format a floating number to a fixed width in Python.

In this article, we’ll look at how to format a floating number to a fixed width in Python.

How to format a floating number to a fixed width in Python?

To format a floating number to a fixed width in Python, we can use a special formatting code with the string’s format method.

For instance, we write:

numbers = [23.23, 0.1233, 1.0, 4.223, 9887.2]

for x in numbers:
    print("{:10.3f}".format(x))

to format all the numbers in numbers to strings with 3 decimal places.

The {:10.3f} formatting code rounds each number to 3 decimal places.

Therefore, we see:

   23.230
    0.123
    1.000
    4.223
9887.200

printed.

Conclusion

To format a floating number to a fixed width in Python, we can use a special formatting code with the string’s format method.

Categories
Python Answers

How to find the total memory used by a Python process?

Sometimes, we want to find the total memory used by a Python process.

In this article, we’ll look at how to find the total memory used by a Python process.

How to find the total memory used by a Python process?

To find the total memory used by a Python process, we can yue the os and psutil modules.

We install the psutil module by running:

pip install psutil

Then, we write:

import os, psutil

process = psutil.Process(os.getpid())
print(process.memory_info().rss)

We use the psutil.Process constructor with the process ID of our script which we get by using os.getpid.

Then we get the memory used with process.memory_info().rss.

Therefore, we should see the number in bytes of the amount of memory used by our script.

Conclusion

To find the total memory used by a Python process, we can yue the os and psutil modules.

Categories
Python Answers

How to convert an int to binary string with Python?

Sometimes, we want to convert an int to binary string with Python .

In this article, we’ll look at how to convert an int to binary string with Python.

How to convert an int to binary string with Python?

To convert an int to binary string with Python, we can use the string’s format method.

For instance, we write:

b = "{0:b}".format(37)
print(b)

We use the {0:b} formatting code to convert an int to a binary string.

Therefore, b is 100101, which is 37 in binary.

Conclusion

To convert an int to binary string with Python, we can use the string’s format method.