Categories
Python Answers

How to download a file over HTTP with Python?

Sometimes, we want to download a file over HTTP with Python.

In this article, we’ll look at how to download a file over HTTP with Python.

How to download a file over HTTP with Python?

To download a file over HTTP with Python, we can use the urllib.request.urlretrieve method.

For instance, we’ll write:

import urllib.request

photo = urllib.request.urlretrieve(
    "https://i.picsum.photos/id/830/200/300.jpg?hmac=YHS3854_x-GHeQToxsiUmEvBJpDbZOAyixX9nxz61Sg",
    "photo.jpg")
print(photo)

We call urllib.request.urlretrieve with the URL of the file to retrieve and the file name to save the file as respectively.

And we assign the returned file to photo.

Therefore, photo is the photo file object we downloaded onto the disk.

Conclusion

To download a file over HTTP with Python, we can use the urllib.request.urlretrieve method.

Categories
Python Answers

How to sort a list of tuples by the element at a given index with Python?

Sometimes, we want to sort a list of tuples by the element at a given index with Python.

In this article, we’ll look at how to sort a list of tuples by the element at a given index with Python.

How to sort a list of tuples by the element at a given index with Python?

To sort a list of tuples by the element at a given index with Python, we can use the sorted function with a lambda function that specifies which item in the tuple we want to sort the list of tuples by.

For instance, we write:

data = [(1, 2, 3), (1, 2, 1), (1, 1, 4)]
sorted_data = sorted(data, key=lambda tup: (tup[1], tup[2]))
print(sorted_data)

to call sorted to sort data by the value of the 2nd and 3rd items in each tuple in this order.

We specify this with a lambda function that returns a tuple with the 2nd and 3rd item in each tuple.

Therefore, data is [(1, 1, 4), (1, 2, 1), (1, 2, 3)].

Conclusion

To sort a list of tuples by the element at a given index with Python, we can use the sorted function with a lambda function that specifies which item in the tuple we want to sort the list of tuples by.

Categories
Python Answers

How to create a dictionary with list comprehension with Python?

Sometimes, we want to create a dictionary with list comprehension with Python.

In this article, we’ll look at how to create a dictionary with list comprehension with Python.

How to create a dictionary with list comprehension with Python?

To create a dictionary with list comprehension with Python, we can use the dictionary comprehension syntax in the following format:

{key: value for (key, value) in iterable}

For instance, we write:

ts = [(1, 2), (3, 4), (5, 6)]
d = {key: value for (key, value) in ts}
print(d)

We have the ts array with tuples as elements.

Then we destructure the tuples with for (key, value) in ts and put them into the dictionary as entries.

And then we assign the dictionary to d.

Therefore, d is {1: 2, 3: 4, 5: 6}.

Conclusion

To create a dictionary with list comprehension with Python, we can use the dictionary comprehension syntax in the following format:

{key: value for (key, value) in iterable}
Categories
Python Answers

How to use if/else in a list comprehension in Python?

Sometimes, we want to use if/else in a list comprehension in Python.

In this article, we’ll look at how to use if/else in a list comprehension in Python.

How to use if/else in a list comprehension in Python?

To use if/else in a list comprehension in Python, we can write it in the following format:

[f(x) if condition else g(x) for x in sequence]

For instance, we write:

x = [1.5, 2.3, 4.4, 5.4, 'n', 1.5, 5.1, 'a']  
x_non_str = [el for el in x if not isinstance(el, str)] 
print(x_non_str)

to use list comprehension and if/else to return an array with non-string elements.

We return anything in x that has not isinstance(el, str) return True.

Therefore, x_non_str is [1.5, 2.3, 4.4, 5.4, 1.5, 5.1].

Conclusion

To use if/else in a list comprehension in Python, we can write it in the following format:

[f(x) if condition else g(x) for x in sequence]
Categories
Python Answers

How to use a regular expression match a whole word with Python?

Sometimes, we want to use a regular expression match a whole word with Python.

In this article, we’ll look at how to use a regular expression match a whole word with Python.

How to use a regular expression match a whole word with Python?

To use a regular expression match a whole word with Python, we can use the re.search method with a regex to match a whole word.

For instance, we write:

import re

x = "this is a sample"
res = re.search(r'\bis\b', x)
print(res)

We call re.search with a regex that matches the word ‘is’.

\b indicates the word boundary.

The 2nd argument is the string that we’re searching for the matches for.

Therefore, res is <re.Match object; span=(5, 7), match='is'>.

Conclusion

To use a regular expression match a whole word with Python, we can use the re.search method with a regex to match a whole word.