Categories
Python Answers

How to check if a given key already exists in a dictionary with Python?

Sometimes, we want to check if a given key already exists in a dictionary with Python.

In this article, we’ll look at how to check if a given key already exists in a dictionary with Python.

How to check if a given key already exists in a dictionary with Python?

To check if a given key already exists in a dictionary with Python, we can use the in operator.

For instance, we write:

d = {"key1": 10, "key2": 23}

if "key1" in d:
    print("has key1")

We have the d dictionary.

And we check if there’s an entry with key 'key1' by writing:

"key1" in d

Since this is True, we see 'has key1' logged.

Conclusion

To check if a given key already exists in a dictionary with Python, we can use the in operator.

Categories
Python Answers

How to split a list into evenly sized chunks with Python?

Sometimes, we want to split a list into evenly sized chunks with Python.

In this article, we’ll look at how to split a list into evenly sized chunks with Python.

How to split a list into evenly sized chunks with Python?

To split a list into evenly sized chunks with Python, we can use list comprehsion.

For instance, we write:

import pprint

lst = list(range(10, 55))
n = 10
chunked = [lst[i:i + n] for i in range(0, len(lst), n)]
pprint.pprint(chunked)

We create the lst list with numbers from 10 to 54.

Then we want to split them to chunks of 10, which we assigned to n.

Then we split the lst list into chunks of size n with:

[lst[i:i + n] for i in range(0, len(lst), n)]

We loop through the lst entries and compute the chunks with lst[i:i + n].

Therefore, chunked is:

[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54]]

Conclusion

To split a list into evenly sized chunks with Python, we can use list comprehsion.

Categories
Python Answers

How to print colored text to the terminal with Python?

Sometimes, we want to print colored text to the terminal with Python.

In this article, we’ll look at how to print colored text to the terminal with Python.

How to print colored text to the terminal with Python?

To print colored text to the terminal with Python, we can surround the string we’re print with the escape code for the color to print.

For instance, we write:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


print(f"{bcolors.WARNING}Warning{bcolors.ENDC}")

to define the bcolors class with some escape codes for various styles to apply.

\033[0m lets us stop applying the given style from this point onward.

Therefore, the text we print should have a brownish color.

Conclusion

To print colored text to the terminal with Python, we can surround the string we’re print with the escape code for the color to print.

Categories
Python Answers

How to parse a string to a float or int with Python?

Sometimes, we want to parse a string to a float or int with Python.

In this article, we’ll look at how to parse a string to a float or int with Python.

How to parse a string to a float or int with Python?

To parse a string to a float or int with Python, we can use the float or int functions respectively.

For instance, we write:

a = "545.2222"
b = float(a)
c = int(b)
print(b)
print(c)

We call float or int with a to convert a to a float or int respectively.

Then we get:

545.2222
545

We have to use the int function with a float.

But we can use float with a number string.

Conclusion

To parse a string to a float or int with Python, we can use the float or int functions respectively.

Categories
Python Answers

How to make function decorators and chain them together with Python?

Sometimes, we want to make function decorators and chain them together with Python.

In this article, we’ll look at how to make function decorators and chain them together with Python.

How to make function decorators and chain them together with Python?

To make function decorators and chain them together with Python, we can create decorators that calls the function that’s in the decorator’s parameter in the wrapper function.

Then we return the wrapper function.

For instance, we write:

def bread(func):
    def wrapper(*args, **kwargs):
        print('bread')
        func(*args, **kwargs)
    return wrapper

def ingredients(func):
    def wrapper(*args, **kwargs):
        print("ingredients")
        func(*args, **kwargs)
    return wrapper

@ingredients
@bread
def sandwich(food):
    print(food)

sandwich('tomato')    

top create the bread and ingredients decorators.

They take the func parameter.

The wrapper function takes an indefinite number of arguments as indicated by *args and **kwargs.

We call func in wrapper with *args and **kwargs and return wrapper so func function, which is modified by the decorator is called.

Then we modify sandwich with the ingredients and bread decorators.

And finally, we call sandwich with tomato.

As a result, we get:

ingredients
bread
tomato

logged in the console.

The decorators’ print functions are called before the one in sandwich is called.

Conclusion

To make function decorators and chain them together with Python, we can create decorators that calls the function that’s in the decorator’s parameter in the wrapper function.

Then we return the wrapper function.