DEV Community

Cover image for Python sets methods explanation and visualizationπŸπŸš€πŸ’‘
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on β€’ Edited on

11 2 1 1 1

Python sets methods explanation and visualizationπŸπŸš€πŸ’‘

Python sets methods

add()

  • Adds an element to the set.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩"}
smileys.add("πŸ˜‡")

# output
{'πŸ˜‰', 'πŸ™‚', 'πŸ˜€', '🀩', 'πŸ˜‡'}
Enter fullscreen mode Exit fullscreen mode

clear()

  • Removes all the elements from the set.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩"}
smileys.clear()

# output
set()
Enter fullscreen mode Exit fullscreen mode

copy()

  • Returns a copy of the set.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩"}
x = smileys.copy()

# output
{'πŸ˜€', 'πŸ˜‰', 'πŸ™‚', '🀩'}
Enter fullscreen mode Exit fullscreen mode

difference()

  • Returns a set containing the difference between two or more sets.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩"}
emojis = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "😍", "🀩", "πŸ˜‡"}
x = emojis.difference(smileys)

# output
{'😍', 'πŸ˜‡'}
Enter fullscreen mode Exit fullscreen mode

difference_update()

  • Removes the items in this set that are also included in another, specified set.
  • The difference_update() method is different from the difference() method, because the difference() method returns a new set.
  • without the unwanted items, and the difference_update() method removes the unwanted items from the original set.
emojis = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "😍", "🀩", "πŸ˜‡"}
smileys = {"πŸ˜€", "πŸ™‚", "🀩"}
emojis.difference_update(smileys)

# output
{'πŸ˜‰', 'πŸ˜‡', '😍'}
Enter fullscreen mode Exit fullscreen mode

discard()

  • Remove the specified item.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩"}
smileys.discard("πŸ˜‰")

# output
{'πŸ˜€', 'πŸ™‚', '🀩'}
Enter fullscreen mode Exit fullscreen mode

intersection()

  • Returns a set, that is the intersection of two or more sets.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩"}
emojis = {"πŸ˜‰", "😍", "πŸ˜‡"}

x = smileys.intersection(emojis)

# output
{'πŸ˜‰'}
Enter fullscreen mode Exit fullscreen mode

intersection_update()

  • method removes the items that is not present in both sets (or in all sets if the comparison is done between more than two sets).
  • The intersection_update() method is different from the intersection() method, because the intersection() method returns a new set.
  • without the unwanted items, and the intersection_update() method removes the unwanted items from the original set.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩", "πŸ˜‡"}
emojis = {"πŸ˜‰", "😍", "πŸ˜‡"}

smileys.difference_update(emojis)

# output
{'🀩', 'πŸ™‚', 'πŸ˜€'}
Enter fullscreen mode Exit fullscreen mode

isdisjoint()

  • Returns whether two sets have a intersection or not
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‡"}
emojis = {"πŸ˜‰", "😍", "🀩"}

x = smileys.isdisjoint(emojis)

# output
True
Enter fullscreen mode Exit fullscreen mode

issubset()

  • method returns True if all items in the set exists in the specified set, otherwise it retuns False.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‡"}
emojis = {"πŸ˜‰", "😍", "🀩"}

x = smileys.issubset(emojis)

# output
False
Enter fullscreen mode Exit fullscreen mode

pop()

  • Removes an element from the set.
  • Note: Because the set() is unordered we cannot ensure what element will be removed.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩", "πŸ˜‡"}
x = smileys.pop()

# output 1
πŸ™‚
# output 2
🀩
# output 3
πŸ˜‰
Enter fullscreen mode Exit fullscreen mode

remove()

  • Removes the specified element.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩", "πŸ˜‡"}
smileys.remove("πŸ˜‰")

# output
{'πŸ˜€', 'πŸ™‚', 'πŸ˜‡', '🀩'}
Enter fullscreen mode Exit fullscreen mode

symmetric_difference()

  • method returns a set that contains all items from both set, but not the items that are present in both sets. -Meaning: The returned set contains a mix of items that are not present in both sets.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩", "πŸ˜‡"}
emojis = {"πŸ˜‰", "😍", "🀩"}

x = smileys.symmetric_difference(emojis)

# output
{'πŸ˜‡', 'πŸ™‚', 'πŸ˜€', '😍'}
Enter fullscreen mode Exit fullscreen mode

symmetric_difference_update()

  • method updates the original set by removing items that are present in both sets, and inserting the other items.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‰", "🀩", "πŸ˜‡"}
emojis = {"πŸ˜‰", "😍", "🀩", "πŸ˜‡"}

smileys.symmetric_difference_update(emojis)

# output
{'😍', 'πŸ™‚', 'πŸ˜€'}
Enter fullscreen mode Exit fullscreen mode

union()

  • method returns a set that contains all items from the original set, and all items from the specified set(s).
  • Note: It will remove the same element from the original set.
smileys = {"πŸ˜€", "πŸ™‚", "πŸ˜‡", "πŸ˜‰"}
emojis = {"πŸ˜‰", "😍", "🀩", "πŸ˜‡"}

x = smileys.union(emojis)

# output
{'πŸ˜‡', 'πŸ˜‰', 'πŸ˜€', 'πŸ™‚', '😍', '🀩'}
Enter fullscreen mode Exit fullscreen mode

update()

  • method updates the current set, by adding items from another set (or any other iterable).
smileys = {"πŸ˜€", "πŸ™‚"}
emojis = {"πŸ˜‰", "😍", "🀩", "πŸ˜‡"}

smileys.update(emojis)

# output
{'😍', 'πŸ™‚', '🀩', 'πŸ˜‡', 'πŸ˜‰', 'πŸ˜€'}
Enter fullscreen mode Exit fullscreen mode

All the best to you.

Connect with Me 😊

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (8)

Collapse
 
mindscopeimagine profile image
Kunle O. Daramola β€’

πŸ‘πŸ‘

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny β€’

Thanks πŸ€—

Collapse
 
claudejeandarm profile image
ClaudeJeanDarm β€’

Hi, any updates on that?

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny β€’

No, there no update

Collapse
 
henry_lee_1787e739b0c8191 profile image
Develop Mastery β€’

nice. i learned a lot from your contents

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny β€’

It's my pleasure this make me very happy 😊

Collapse
 
xeroq profile image
AnΓ­bal SΓ‘nchez Numa β€’

Nice and clear.

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny β€’

It's my pleasure, Thank you.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more