Avatar ·

Convert some elements from a list to int

📁 List, Advanced Python Topics, example

Please tell me, is there a shorter way to convert elements to int type?
For example, given a list a = ["1", "2", "hello", "3", "hello"]
Is it possible to write this conversion more concisely, but not like this:

a[0] = int(a[0])a[1] = int(a[1])a[3] = int(a[3])

The list is actually longer, I don't want to make such a column in the code.

Avatar ·

To get the result of a command as a string, it is not necessary to call cmd.exe; you can directly run a child process:

import subprocessoutput = subprocess.check_output('ipconfig')

To "save to notepad", simply write the result to a file:

from pathlib import PathPath('~/output.txt').expanduser().write_bytes(output)

Instead of the given path (saves the file output.txt to the user's home directory), you can use a program-specific directory ().

You can do it in one step: call the command and save

Log in to leave an answer

Blogs