Featured Post

Scraping Website: How to Write a Script in Python

Here's a python script that you can use as a model to scrape a website. Python script The below logic uses BeautifulSoup Package for web scraping. import requests from bs4 import BeautifulSoup url = 'https://www.example.com' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # Print the title of the webpage print(soup.title.text) # Print all the links in the webpage for link in soup.find_all('a'):     print(link.get('href')) In this script, we first import the Requests and Beautiful Soup libraries. We then define the URL we want to scrape and use the Requests library to send a GET request to that URL. We then pass the response text to Beautiful Soup to parse the HTML contents of the webpage. We then use Beautiful Soup to extract the title of the webpage and print it to the console. We also use a for loop to find all the links in the webpage and print their href attributes to the console. This is just a basic example, but

Python Set comprehension - How to Use it Read now

set comprehension

Set comprehension in Python will not allow duplicates.

You can't modify an existing set with a comprehension. But you can create a new one. Set comprehension the prime purpose is to create new set.

Set Comprehension 

Likewise, of course, the comprehension must result in a valid set. A set cannot contain multiple entries of the same value. Like the dictionary, Python is polite about this. 

If you try to add values to the set that are already there, it will replace the old one with the new one.

Curling brackets- Set comprehension

Set comprehensions using the {} syntax only exist in Python 3. Before that, you'll have to use the set() function to create and work with sets. You might guess, therefore, that one of the best uses of a set is to eliminate duplicates.

In fact, this is one of the most basic forms of set comprehension. Given a list, we can duplicate it as a list with a simple list comprehension like this:

List logic

if we change the list comprehension to a set comprehension, we get the same result, but as a set. That means without duplicates.

list_copy = [x for x in original_list]


Set comprehension

my_list_with_dupes = [1,2,1,2,3,4,1,2,3,4,5,6,7,1,2,3] 
my_set_without_dupes = {x for x in my_list_with_dupes} 

{1, 2, 3, 4, 5, 6, 7}

Related posts


Popular posts from this blog

7 AWS Interview Questions asked in Infosys, TCS

How to Decode TLV Quickly