-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from realpython/python-set-comprehension
Sample code for the article on set comprehension
- Loading branch information
Showing
8 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Python Set Comprehensions: How and When to Use Them | ||
|
||
This folder provides the code examples for the Real Python tutorial [Python Set Comprehensions: How and When to Use Them](https://realpython.com/python-set-comprehension/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
colors = {"blue", "red", "green", "orange", "green"} | ||
print(colors) | ||
colors.add("purple") | ||
print(colors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
|
||
print({number**2 if number % 2 == 0 else number**3 for number in numbers}) | ||
|
||
result_set = set() | ||
for number in numbers: | ||
if number % 2 == 0: | ||
value = number**2 | ||
else: | ||
value = number**3 | ||
result_set.add(value) | ||
|
||
print(result_set) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
emails = { | ||
" [email protected] ", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
" [email protected]", | ||
"[email protected]", | ||
} | ||
|
||
print({email.strip().lower() for email in emails}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
emails_set = { | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
} | ||
|
||
print({email for email in emails_set if email.endswith(".com")}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
matrix = [ | ||
[9, 3, 8, 3], | ||
[4, 5, 2, 8], | ||
[6, 4, 3, 1], | ||
[1, 0, 4, 5], | ||
] | ||
|
||
print({value**2 for row in matrix for value in row}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
unique_words = set() | ||
text = """ | ||
Beautiful is better than ugly | ||
Explicit is better than implicit | ||
Simple is better than complex | ||
Complex is better than complicated | ||
""".lower() | ||
|
||
for word in text.split(): | ||
unique_words.add(word) | ||
|
||
print(unique_words) | ||
|
||
print(set(text.split())) | ||
|
||
unique_words = {word for word in text.split()} | ||
|
||
print(unique_words) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
tools = ["Python", "Django", "Flask", "pandas", "NumPy"] | ||
tools_set = {tool.lower() for tool in tools} | ||
|
||
print(tools_set) | ||
print("python".lower() in tools_set) | ||
print("Pandas".lower() in tools_set) | ||
print("Numpy".lower() in tools_set) |