Categories
Python Answers

How to find intersection of two nested lists with Python?

Sometimes, we want to find intersection of two nested lists with Python.

In this article, we’ll look at how to find intersection of two nested lists with Python.

How to find intersection of two nested lists with Python?

To find intersection of two nested lists with Python, we can call the set’s intersection method.

For instance, we write

b1 = [1, 2, 3, 4, 5, 9, 11, 15]
b2 = [4, 5, 6, 7, 8]
s = set(b1).intersection(b2)

to create a set from the b1 list.

And then we call intersection with the b2 list to return a set that has the elements in both lists.

Conclusion

To find intersection of two nested lists with Python, we can call the set’s intersection method.

Categories
Python Answers

How to declare custom exceptions in Python?

Sometimes, we want to declare custom exceptions in Python.

In this article, we’ll look at how to declare custom exceptions in Python.

How to declare custom exceptions in Python?

To declare custom exceptions in Python, we can create a subclass of the Exception class.

For instance, we write

class ValidationError(Exception):
    def __init__(self, message, errors):                    
        super().__init__(message)
        self.errors = errors

to create the ValidationError exception class that’s a subclass of Exception.

In the __init__ method, we call the Exception constructor with

super().__init__(message)

Then we add custom code after that like

self.errors = errors

Conclusion

To declare custom exceptions in Python, we can create a subclass of the Exception class.

Categories
Python Answers

How to suppress scientific notation when printing float values with Python?

Sometimes, we want to suppress scientific notation when printing float values with Python.

In this article, we’ll look at how to suppress scientific notation when printing float values with Python.

How to suppress scientific notation when printing float values with Python?

To suppress scientific notation when printing float values with Python, we call the format method.

For instance, we write

a = -7.1855143557448603e-20
'{:f}'.format(a)

to call format on string '{:f}' to return a string that has all the digits in the number a in it.

Conclusion

To suppress scientific notation when printing float values with Python, we call the format method.

Categories
Python Answers

How to have multiple prints on the same line in Python?

Sometimes, we want to have multiple prints on the same line in Python

In this article, we’ll look at how to have multiple prints on the same line in Python.

How to have multiple prints on the same line in Python?

To have multiple prints on the same line in Python, we can set the end argument of print to an empty string.

For instance, we write

def install():
    print("Installing...      ", end="", flush=True)

install()
print("[DONE]")

to call print with a string and the end argument set to an empty string in the install function.

Then we call install function to print "Installing... ".

And then we call print again to to print "[DONE]" on the same line since end is set to an empty string instead of a new line.

Conclusion

To have multiple prints on the same line in Python, we can set the end argument of print to an empty string.

Categories
Python Answers

How to split a list based on a condition with Python?

Sometimes, we want to split a list based on a condition with Python.

In this article, we’ll look at how to split a list based on a condition with Python.

How to split a list based on a condition with Python?

To split a list based on a condition with Python, we can use list comprehension.

For instance, we write

good = [x for x in mylist if x in goodvals]
bad  = [x for x in mylist if x not in goodvals]

to create the good list with the values in the goodvals list with

[x for x in mylist if x in goodvals]

Likewise, we create the bad list with the values that aren’t in goodvals with

[x for x in mylist if x not in goodvals]

Conclusion

To split a list based on a condition with Python, we can use list comprehension.