Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sicherheitslücken (CVE)5 ways AI is reshaping the cybersecurity job market(21.09.2026 um 10:25 Uhr)
IT Security NachrichtenRevoking the token didn’t kill the backdoor(21.09.2026 um 11:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT per Polygon: ClickFix-Kampagnen drehen C2-Infrastruktur(21.09.2026 um 10:55 Uhr)
IT Security NachrichtenEnterprise Mobile KI: So lassen sich Shadow-AI-Risiken kontrollieren(21.09.2026 um 12:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT setzt auf Polygon-Blockchain für C2-Rotation(21.09.2026 um 12:19 Uhr)
Sicherheitslücken (CVE)5 ways AI is reshaping the cybersecurity job market(21.09.2026 um 10:25 Uhr)
IT Security NachrichtenRevoking the token didn’t kill the backdoor(21.09.2026 um 11:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT per Polygon: ClickFix-Kampagnen drehen C2-Infrastruktur(21.09.2026 um 10:55 Uhr)
IT Security NachrichtenEnterprise Mobile KI: So lassen sich Shadow-AI-Risiken kontrollieren(21.09.2026 um 12:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT setzt auf Polygon-Blockchain für C2-Rotation(21.09.2026 um 12:19 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Python Sets: remove() vs discard() — When Silence Is Golden

Python sets have two methods for removing elements: remove() and discard(). They look identical: colors = {"red", "green", "blue"} colors.remove("green") # Works colors.discard("green") # Also works But watch what happens…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Python sets have two methods for removing elements: remove() and discard().



They look identical:




colors = {"red", "green", "blue"}
colors.remove("green") # Works
colors.discard("green") # Also works






But watch what happens when the element doesn't exist:




colors = {"red", "blue"}

colors.remove("yellow") # KeyError: 'yellow'
colors.discard("yellow") # Nothing happens. No error.






remove() raises an exception. discard() fails silently.






When to Use Each



Use remove() when missing elements are bugs:




def deactivate_user(active_users, user_id):
active_users.remove(user_id) # Should exist!
# If it doesn't, something's wrong upstream






If user_id isn't in active_users, that's a logic error in your code. You want the exception to surface it.



Use discard() when missing elements are expected:




def cleanup_session(sessions, session_id):
sessions.discard(session_id) # May already be gone
# User might have logged out twice, that's fine






Here, the session might have already been cleaned up. No need to crash over it.






The AI Engineering Context



This pattern appears constantly in data pipelines:




# Processing documents for RAG
processed_ids = set()

for doc in document_stream():
if doc.id in processed_ids:
continue

processed_ids.add(doc.id)
process(doc)

# Cleanup: remove from pending queue
pending_queue.discard(doc.id) # Might not be there






Using discard() here prevents crashes when the same document appears in multiple streams.






The Decision Framework

































Scenario Use Because
Element should exist remove() Missing = bug, surface it
Element might exist discard() Missing = expected, ignore it
Idempotent operations discard() Safe to call multiple times
Strict state management remove() Catch invalid transitions





One More Option



If you want to know whether removal happened without raising an error, combine in with discard():




if element in my_set:
my_set.discard(element)
print("Removed")
else:
print("Wasn't there")






Though this is rarely necessary—usually you either care (use remove()) or you don't (use discard() and move on).



Which do you default to? Do you prefer loud failures or silent forgiveness?






This is adapted from my upcoming book, Zero to AI Engineer: Python Foundations.

I share excerpts like this on Substack → https://substack.com/@samuelochaba

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Python Sets: remove() vs discard() — When Silence Is Golden

Thematisch verwandte Begriffe: Python, Sets, remove, discard · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94036 | A security flaw has been discovered in D-Link DIR-X1860 and DIR-X1860Z u…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick