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?
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?
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
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.