-1

I saw others use

CurlyBraces = {'Lorem','Ipsum'}

for their tables in python, but some others use

SquareBrackets = ['Lorem','Ipsum']

for their tables, what's the difference?

Beedful
  • 115
  • 2
  • 11
  • https://stackoverflow.com/questions/9197324/what-is-the-meaning-of-curly-braces – Stefan Nov 30 '21 at 06:24
  • 3
    First one is a set, it accepts only hashable objects, is unordered, and keeps only one occurrence of each object. The second one is a list, is ordered, can contain pretty much everything. – mozway Nov 30 '21 at 06:24
  • 1
    Beware when you see a var defined like `{}` - it's a dict, not a set. If you want to declare an empty set you have to use an explicit constructor like `set()` instead. – rv.kvetch Nov 30 '21 at 06:31

2 Answers2

0

Curly Braces creates a Set (https://www.w3schools.com/python/python_sets.asp) or Dictionary, while square braces creates List (https://www.w3schools.com/python/python_lists.asp).

A set is an unordered data structure, best for finding items fast, while a list is an ordered data structure

0

The first one is a shorthand for a set, and the second for a list. You could also have one very similar to the first to define a dict.

For example, here are 2 equivalent definitions for each:

# list
my_list_1 = [1, 2, "cat"]

my_list_2 = list()
my_list_2.append(1)
my_list_2.append(2)
my_list_2.append("cat")


# set
my_set_1 = {1, 2, "cat"}

my_set_2.add(1)
my_set_2.add(2)
my_set_2.add("cat")


# dict
my_dict_1 = {"animal": "cat", 42: "number", "something": 1}

my_dict_2["animal"] = "cat"
my_dict_2[42] = "number"
my_dict_2["something"] = 1

Also note that if you see some_var = {}, it means a dict. Sets are more rare in Python.

Morton
  • 160
  • 1
  • 10