Categories
Python Answers

How to flatten a shallow list in Python?

Sometimes, we want to flatten a shallow list in Python.

In this article, we’ll look at how to flatten a shallow list in Python.

How to flatten a shallow list in Python?

To flatten a shallow list in Python, we can use the itertools.chain method.

For instance, we write:

import itertools

list_of_menuitems = [['image00', 'image01'], ['image10'], []]

chain = itertools.chain(*list_of_menuitems)
print(list(chain))

We defined the list_of_menuitems list which has lists inside it.

Then we call itertools.chain with the list_of_menuitems used as arguments since we spread it with *.

And then we convert the returned chain iterator to a list with list.

Therefore, we see:

['image00', 'image01', 'image10']

printed.

Conclusion

To flatten a shallow list in Python, we can use the itertools.chain method.

Categories
Python Answers

How to keep dictionary keys/values in same order as declared with Python?

Sometimes, we want to keep dictionary keys/values in same order as declared with Python.

In this article, we’ll look at how to keep dictionary keys/values in same order as declared with Python.

How to keep dictionary keys/values in same order as declared with Python?

To keep dictionary keys/values in same order as declared with Python, we can use the OrderDict class to create our dictionary.

For instance, we write:

from collections import OrderedDict

d = {'ac': 33, 'gw': 20, 'ap': 102, 'za': 321, 'bs': 10}
ordered_d = OrderedDict(d)
print(ordered_d)

We import the OrderedDict class from the collections module.

Then we declared a regular dictionary and assigned it to d.

Next, we use d as the argument of OrderedDict and assigned it to ordered_d.

Therefore, ordered_d is:

OrderedDict([('ac', 33), ('gw', 20), ('ap', 102), ('za', 321), ('bs', 10)])

Conclusion

To keep dictionary keys/values in same order as declared with Python, we can use the OrderDict class to create our dictionary.

Categories
Python Answers

How to run a shell command and capturing the output with Python?

Sometimes, we want to run a shell command and capturing the output with Python.

In this article, we’ll look at how to run a shell command and capturing the output with Python.

How to run a shell command and capturing the output with Python?

To run a shell command and capturing the output with Python, we can use the subprocess.run method.

For instance, we write:

import subprocess

result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)
print(result.stdout)

We call subprocess.run with an array that has the command string with its arguments.

And we set stdout to subprocess.PIPE to pipe the input to stdout.

We then get the output from the result.stdout property as a binary string.

As a result, we get something like:

b'total 28\n-rw-r--r-- 1 runner runner    5 Oct 16 20:15 file\n-rw-r--r-- 1 runner runner   13 Oct 16 01:41 foo.txt\n-rw-r--r-- 1 runner runner  102 Oct 17 01:58 main.py\n-rw-r--r-- 1 runner runner 4540 Oct 16 22:17 photo.jpg\n-rw-r--r-- 1 runner runner 3449 Oct 16 22:24 poetry.lock\ndrwxr-xr-x 1 runner runner   36 Oct 15 23:31 __pycache__\n-rw-r--r-- 1 runner runner  358 Oct 16 22:24 pyproject.toml\n'

as the value of result.stdout.

Conclusion

To run a shell command and capturing the output with Python, we can use the subprocess.run method.

Categories
Python Answers

How to get all possible combinations of a list’s elements with Python?

Sometimes, we want to get all possible combinations of a list’s elements with Python.

In this article, we’ll look at how to get all possible combinations of a list’s elements with Python.

How to get all possible combinations of a list’s elements with Python?

To get all possible combinations of a list’s elements with Python, we can use the itertools.combinations method.

For instance, we write:

import itertools

stuff = [1, 2, 3]
for L in range(0, len(stuff) + 1):
    for subset in itertools.combinations(stuff, L):
        print(subset)

We loop through number ranges from 0 to len(stuff) + 1.

In the loop body, we get the combination of stuff when we choose L items with itertools.combinations.

And then we loop through the returned iterator with another for loop.

In the loop body, we print the subset of items from stuff that are chosen, which are stored in a tuple.

Therefore, we see:

()
(1,)
(2,)
(3,)
(1, 2)
(1, 3)
(2, 3)
(1, 2, 3)

printed.

Conclusion

To get all possible combinations of a list’s elements with Python, we can use the itertools.combinations method.

Categories
Python Answers

How to generate all permutations of a list with Python?

Sometimes, we want to generate all permutations of a list with Python.

In this article, we’ll look at how to generate all permutations of a list with Python.

How to generate all permutations of a list with Python?

To generate all permutations of a list with Python, we can use the itertools.permutations method.

For instance, we write:

import itertools

perms = list(itertools.permutations([1, 2, 3]))
print(perms)

We call itertools.permtations with a list to get all the permutations from.

It returns a iterator with all the permutations of [1, 2, 3].

Next, we convert the iterator to a list with list and assign it to perms.

Therefore, perms is:

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

Conclusion

To generate all permutations of a list with Python, we can use the itertools.permutations method.