Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1229803

Browse filesBrowse files
Merge pull request DahlitzFlorian#6 from DahlitzFlorian/empty-set-literal
empty_set_literal - Create an empty set using a literal
2 parents b6d3b0d + 8e8b06f commit 1229803
Copy full SHA for 1229803

File tree

6 files changed

+22
-0
lines changed
Filter options

6 files changed

+22
-0
lines changed

‎ebook/chapters/standard_lib_chapters/non_categorized.tex

Copy file name to clipboardExpand all lines: ebook/chapters/standard_lib_chapters/non_categorized.tex
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,22 @@ \subsection{Count Elements Between Thresholds}
237237
$ python count_thresholds.py
238238
3
239239
\end{lstlisting}
240+
241+
242+
\subsection{Empty Set Literal}
243+
244+
People, who are new to Python, meeting the built-in set type for the first time often get confused when trying to create an empty set by using the \lstinline|{}| literal.
245+
This literal is already reserved for creating empty dictionaries.
246+
The normal way to create a new empty set in Python is to use the \lstinline{set()} constructor.
247+
However, if you like, you can use a literal, too!
248+
249+
\lstinputlisting[caption=empty\_set\_literal.py]{../standard_lib/empty_set_literal.py}
250+
251+
What happens here is that an empty list (using the list literal \lstinline{[]}) is created and unpacked using the \lstinline{*}-operator.
252+
The elements from the unpacked empty list are passed to the curly braces.
253+
Even though no elements at all are passed to the set literal, Python accepts it as valid set creation syntax.
254+
255+
\begin{lstlisting}[caption=Output of empty\_set\_literal.py]
256+
$ python empty_set_literal.py
257+
set()
258+
\end{lstlisting}

‎ebook/python-snippets.epub

Copy file name to clipboard
440 Bytes
Binary file not shown.

‎ebook/python-snippets.mobi

Copy file name to clipboard
400 Bytes
Binary file not shown.

‎ebook/python-snippets.pdf

Copy file name to clipboard
1.79 KB
Binary file not shown.

‎standard_lib/README.md

Copy file name to clipboardExpand all lines: standard_lib/README.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ A collection of useful snippets using only the standard library.
2121
| dict_based_on_lists | Create a dict based on two lists |
2222
| disassemble_bytecode | Shows the bytecode representation of f-strings and str() conversion of integers |
2323
| drawing_turtle | Drawing a dragon symbol using Python's built-in turtle |
24+
| empty_set_literal | Create an empty set using a literal |
2425
| emulate_switch_case | Emulate switch-case-statements as they don't exist in Python |
2526
| file_matching_regex | Find matching files using `re` and `fnmatch` |
2627
| fill_zeros | Fill strings using leading zeros. |

‎standard_lib/empty_set_literal.py

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
empty_set = {*[]}
2+
print(empty_set)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.