Use curly braces to initialize a Set in Python

前端 未结 4 1200
别跟我提以往
别跟我提以往 2020-12-13 11:47

I\'m learning python, and I have a novice question about initializing sets. Through testing, I\'ve discovered that a set can be initialized like so:

my_set          


        
4条回答
  •  误落风尘
    2020-12-13 12:29

    There are two obvious issues with the set literal syntax:

    my_set = {'foo', 'bar', 'baz'}
    
    1. It's not available before Python 2.7

    2. There's no way to express an empty set using that syntax (using {} creates an empty dict)

    Those may or may not be important to you.

    The section of the docs outlining this syntax is here.

提交回复
热议问题