0

I wanted to learn more of python and came to the command ".update()". I googled it up and really tried to understood, however it's randomized.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.update(y) 

print(x)

this is my code and there are 2 things I could see:

  1. The output gives me 5 words and somehow just one is missing
  2. The words are getting chosen randomly

Did I do something wrong? Why is there just one word missing and not 2/3/4?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    _None_ are missing, the set contains all five unique words. And sets aren't semantically ordered data structures, the order is arbitrary (note not necessarily random). Probably you don't actually want to use a set at all, if either part of that behaviour is surprising. – jonrsharpe Apr 26 '22 at 14:52
  • Those are sets. I suggest you read the python set [documentation](https://docs.python.org/3.8/library/stdtypes.html#set) – John Gordon Apr 26 '22 at 14:54

1 Answers1

0

x and y are the sets. Set is a type of data structure that keeps only unique values. So, when you update x with y, x set will contain unique values from both sets.

Andrey Lukyanenko
  • 3,679
  • 2
  • 18
  • 21