Understanding the set() function

前端 未结 5 786
故里飘歌
故里飘歌 2020-11-29 02:07

In python, set() is an unordered collection with no duplicate elements. However, I am not able to understand how it generates the output.

For example,

5条回答
  •  鱼传尺愫
    2020-11-29 02:27

    After reading the other answers, I still had trouble understanding why the set comes out un-ordered.

    Mentioned this to my partner and he came up with this metaphor: take marbles. You put them in a tube a tad wider than marble width : you have a list. A set, however, is a bag. Even though you feed the marbles one-by-one into the bag; when you pour them from a bag back into the tube, they will not be in the same order (because they got all mixed up in a bag).

提交回复
热议问题