Categories
Python Answers

How to split string with multiple delimiters in Python?

Sometimes, we want to split string with multiple delimiters in Python.

In this article, we’ll look at how to split string with multiple delimiters in Python.

How to split string with multiple delimiters in Python?

To split string with multiple delimiters in Python, we can use the re.split method.

For instance, we write

import re

a = 'Quick, brown; fox jumped*over\na dog'
re.split('; |, |\*|\n', a)

to call re.split with a string with the list of symbols we want to split by and the a string to split the string by the symbols listed.

Conclusion

To split string with multiple delimiters in Python, we can use the re.split method.

Categories
Python Answers

How to print number with commas as thousands separators with Python?

Sometimes, we want to print number with commas as thousands separators with Python.

In this article, we’ll look at how to print number with commas as thousands separators with Python.

How to print number with commas as thousands separators with Python?

To print number with commas as thousands separators with Python, we can use a format string with the locale library.

For instance, we write

import locale
locale.setlocale(locale.LC_ALL, '') 

print(f'{value:n}')

to set the locale with

locale.setlocale(locale.LC_ALL, '') 

Then we format the value number to a number string with thousands separators with

f'{value:n}'

Conclusion

To print number with commas as thousands separators with Python, we can use a format string with the locale library.

Categories
Python Answers

How to remove all the elements that occur in one list from another with Python?

Sometimes, we want to remove all the elements that occur in one list from another with Python.

In this article, we’ll look at how to remove all the elements that occur in one list from another with Python.

How to remove all the elements that occur in one list from another with Python?

To remove all the elements that occur in one list from another with Python, we can use list comprehension.

For instance, we write

l3 = [x for x in l1 if x not in l2]

to return a list of all items in l1 that isn’t in l2.

And we assign the returned list to l3.

Conclusion

To remove all the elements that occur in one list from another with Python, we can use list comprehension.

Categories
Python Answers

How to retrieve links from web page using Python and BeautifulSoup?

Sometimes, we want to retrieve links from web page using Python and BeautifulSoup.

In this article, we’ll look at how to retrieve links from web page using Python and BeautifulSoup.

How to retrieve links from web page using Python and BeautifulSoup?

To retrieve links from web page using Python and BeautifulSoup, we can use the SoupStrainer class.

For instance, we write

import httplib2
from bs4 import BeautifulSoup, SoupStrainer

http = httplib2.Http()
status, response = http.request('http://www.example.com')

for link in BeautifulSoup(response, parse_only=SoupStrainer('a')):
    if link.has_attr('href'):
        print(link['href'])

to make a GET request to example.com with

http = httplib2.Http()
status, response = http.request('http://www.example.com')

Then we parse the response by passing it into BeautifulSoup.

And we get the anchor elements by setting the parse_only argument to SoupStrainer('a').

In the loop, we loop through all the links and get the href attribute of each link with attr.

Categories
Python Answers

How to remove all occurrences of a value from a list with Python?

Sometimes, we want to remove all occurrences of a value from a list with Python.

In this article, we’ll look at how to remove all occurrences of a value from a list with Python.

How to remove all occurrences of a value from a list with Python?

To remove all occurrences of a value from a list with Python, we can use the filter function.

For instance, we write

x = [
    1,
    2,
    3,
    2,
    2,
    2,
    3,
    4,
    ]
y = list(filter(lambda a: a != 2, x))

to call filter with a function that filters out all values in x that isn’t 2 and x itself.

We convert the iterable object returned by filter into a list with list.

Conclusion

To remove all occurrences of a value from a list with Python, we can use the filter function.