Categories
Python Answers

How to find the average of a list with Python?

Sometimes, we want to find the average of a list with Python.

In this article, we’ll look at how to find the average of a list with Python.

How to find the average of a list with Python?

To find the average of a list with Python, we can use the statistics.mean method.

For instance, we write:

import statistics

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

avg = statistics.mean(l)
print(avg)

We call statistics.mean with l to calculate the average of the entries in l and return it.

Therefore, avg is 20.11111111111111.

Conclusion

To find the average of a list with Python, we can use the statistics.mean method.

Categories
Python Answers

How to filter a dict to contain only certain keys with Python?

Sometimes, we want to filter a dict to contain only certain keys with Python.

In this article, we’ll look at how to filter a dict to contain only certain keys with Python.

How to filter a dict to contain only certain keys with Python?

To filter a dict to contain only certain keys with Python, we can use dictionary comprehension.

For instance, we write:

d = {'foo': 1, 'foobar': 2, 'bar': 3}

foodict = {k: v for k, v in d.items() if k.startswith('foo')}
print(foodict)

We have a dict d with a few keys.

And we want to create a dict that has the entries with keys that starts with 'foo'.

To do this, we loop through the items with for k, v in d.items().

And then we call k.startswith('foo') to return only the entries that starts with 'foo'.

Therefore, foodict is {'foo': 1, 'foobar': 2}.

Conclusion

To filter a dict to contain only certain keys with Python, we can use dictionary comprehension.

Categories
Python Answers

How to get an absolute file path in Python?

Sometimes, we want to get an absolute file path in Python.

In this article, we’ll look at how to get an absolute file path in Python.

How to get an absolute file path in Python?

To get an absolute file path in Python, we can use the os.path.abspath method.

For instance, we write:

import os

p = os.path.abspath("/foo.txt")
print(p)

We call os.path.abspath with the relative path to the file and returns the absolute path.

Then we get that p is something like '/foo.txt'.

Conclusion

To get an absolute file path in Python, we can use the os.path.abspath method.

Categories
Python Answers

How to generate random integers between 0 and 9 with Python?

Sometimes, we want to generate random integers between 0 and 9 with Python.

In this article, we’ll look at how to generate random integers between 0 and 9 with Python.

How to generate random integers between 0 and 9 with Python?

To generate random integers between 0 and 9 with Python, we can use the randrange function from the random module.

For instance, we write:

from random import randrange

print(randrange(10))

We call randrange with 10 to generate a random number between 0 and 9 and return it.

Conclusion

To generate random integers between 0 and 9 with Python, we can use the randrange function from the random module.

Categories
Python Answers

How to get live output from the subprocess command with Python?

Sometimes, we want to get live output from the subprocess command with Python.

In this article, we’ll look at how to get live output from the subprocess command with Python.

How to get live output from the subprocess command with Python?

To get live output from the subprocess command with Python, we can open a file with open and write to it with write.

And sys.stdout.buffer.write will write the output to the screen.

For instance, we write:

import subprocess
import sys
with open('test.log', 'wb') as f:
    process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
    for c in iter(lambda: process.stdout.read(1), b''):
        sys.stdout.buffer.write(c)
        f.write(c)

We open the file with open.

Then we call subprocess.POpen with the command and argument in a list and set stdout to subprocess.PIPE.

Next, we loop through the iterator that we get from call iter with process.stdout.read(1) to read the live output and loop through them.

In the loop body, we call sys.stdout.buffer.write to write the output to the screen and f.write to write the output to test.log

Therefore, we get something like:

total 20
-rw-r--r-- 1 runner runner   19 Oct 22 23:52 foo.csv
-rw-r--r-- 1 runner runner  242 Oct 23 16:45 main.py
-rw-r--r-- 1 runner runner    4 Oct 23 16:24 out.txt
-rw-r--r-- 1 runner runner 1473 Oct 23 16:28 poetry.lock
-rw-r--r-- 1 runner runner  312 Oct 23 16:28 pyproject.toml
-rw-r--r-- 1 runner runner    0 Oct 23 16:52 test.log

written to the screen and to the file.

Conclusion

To get live output from the subprocess command with Python, we can open a file with open and write to it with write.

And sys.stdout.buffer.write will write the output to the screen.