Categories
Python Answers

How to find and replace elements in a list with Python?

Sometimes, we want to find and replace elements in a list with Python.

In this article, we’ll look at how to find and replace elements in a list with Python.

How to find and replace elements in a list with Python?

To find and replace elements in a list with Python, we can use list comprehension.

For instance, we write:

a = [1, 2, 3, 1, 3, 2, 1, 1]
b = [100 if x == 1 else x for x in a]
print(b)

We have list a and we want to replace all the 1’s with 100.

To do this, we write [100 if x == 1 else x for x in a].

We check if x is 1 where x is each entry in a.

We put 100 in the returned array if x is 1. Otherwise, we put x in the array.

Then we assign the returned array to b.

Therefore b is [100, 2, 3, 100, 3, 2, 100, 100].

Conclusion

To find and replace elements in a list with Python, we can use list comprehension.

Categories
Python Answers

How to properly ignore exceptions in Python?

Sometimes, we want to properly ignore exceptions in Python.

In this article, we’ll look at how to properly ignore exceptions in Python.

How to properly ignore exceptions in Python?

To properly ignore exceptions in Python, we can use the pass keyword in the except clause.

For instance, we write:

try:
    raise Exception()
except:
    pass

to create an empty except clause with pass.

Conclusion

To properly ignore exceptions in Python, we can use the pass keyword in the except clause.

Categories
Python Answers

How to remove an element from a list by index with Python?

Sometimes, we want to remove an element from a list by index with Python.

In this article, we’ll look at how to remove an element from a list by index with Python.

How to remove an element from a list by index with Python?

To remove an element from a list by index with Python, we can use the del operator.

For instance, we write:

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del a[-1]
print(a)

We have the list a and we want to remove the last item.

To do that, we write del a[-1] to get the last item with index -1 and use the del operator to remove it.

Therefore, a is now [0, 1, 2, 3, 4, 5, 6, 7, 8] according to the print output.

Conclusion

To remove an element from a list by index with Python, we can use the del operator.

Categories
Python Answers

How to generate a random string with upper case letters and digits with Python?

Sometimes, we want to generate a random string with upper case letters and digits with Python.

In this article, we’ll look at how to generate a random string with upper case letters and digits with Python.

How to generate a random string with upper case letters and digits with Python?

To generate a random string with upper case letters and digits with Python, we can use the string join method with random.choices and string.ascii_uppercase and string.digits.

For instance, we write:

import string
import random

N = 10
s = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
print(s)

We call random.choices to generate a random string of characters with upper case and digits as specified by string.ascii_uppercase + string.digits.

k is the number of characters in the string which is N.

We call ''.join to combine the list of chosen characters into a string.

Therefore, s is 'LUG43QPVCR'.

Conclusion

To generate a random string with upper case letters and digits with Python, we can use the string join method with random.choices and string.ascii_uppercase and string.digits.

Categories
Python Answers

How to strip HTML from strings in Python?

Sometimes, we want to strip HTML from strings in Python.

In this article, we’ll look at how to strip HTML from strings in Python.

How to strip HTML from strings in Python?

To strip HTML from strings in Python, we can use the StringIO and HTMLParser modules.

For instance, we write:

from io import StringIO
from html.parser import HTMLParser


class MLStripper(HTMLParser):
    def __init__(self):
        super().__init__()
        self.reset()
        self.strict = False
        self.convert_charrefs = True
        self.text = StringIO()

    def handle_data(self, d):
        self.text.write(d)

    def get_data(self):
        return self.text.getvalue()


def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()


print(strip_tags('<p>hello world</p>'))

We create the MLStripper class with the constructor setting the options for parsing HTML.

convert_charrefs converts all characters to Unicode characters.

text sets the source of the text.

In the handle_data method we write the converted text with text_write.

And we return the result in get_data.

Next, we create the strip_tags function that creates a new MLStripper instance.

Then we call s.feed with html to strip the tags off the html string.

And then we return the stripped string that we retrieved from get_data.

Therefore, the print function should print ‘hello world’.

Conclusion

To strip HTML from strings in Python, we can use the StringIO and HTMLParser modules.