Are Python sets mutable

0 votes

In other words, if I do this:

x = set([4, 5, 6])
y = x

y |= set([7, 8, 9])

Are x and y still pointing to the same object, or was a new set created and assigned to y?

Feb 18, 2022 in Python by surbhi
• 3,820 points
606 views

1 answer to this question.

0 votes
>>>> x = set([4, 5, 6])
  • Sets are unordered.

  • Set elements are unique.

  • It is not permitted to use duplicate elements.

  • A set's elements can be changed, but the set's elements must be of an immutable type.

set1 = {4,5,6}

set2 = {4,5,[4,5]}  --> unhashable type: 'list'
# Set elements should be immutable.

Hence, we can see that sets are mutable.

answered Feb 18, 2022 by CoolCoder
• 4,420 points

Related Questions In Python

0 votes
1 answer

Are Python sets mutable?

Python sets are data structures which are ...READ MORE

answered Feb 17, 2022 in Python by Dev
• 6,000 points
782 views
0 votes
1 answer

What are frozen sets in python?

A frozen set in Python is a ...READ MORE

answered Jun 11, 2019 in Python by Nisa
• 1,090 points
2,938 views
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
868 views
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
1,043 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,126 views
0 votes
1 answer

What is the python keyword "with" used for?

It's useful when you have two or ...READ MORE

answered Feb 9, 2022 in Python by CoolCoder
• 4,420 points
713 views
0 votes
1 answer

When to use "while" or "for" in Python

Yes, there is a significant distinction between ...READ MORE

answered Feb 9, 2022 in Python by CoolCoder
• 4,420 points
521 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP