That creates a list of one element with just the node object. Presumably results is expected to be a list.

Answer from vonbrand on Stack Overflow
Discussions

double square brackets side by side in python - Stack Overflow
I'm completely new at Python and have an assignment coming up. The professor has asked that we look at examples of users coding Pascal's Triangle in Python for something that will be 'similar'. I More on stackoverflow.com
๐ŸŒ stackoverflow.com
function - Python variable in the square brackets does not work - Stack Overflow
My goal is to create basic program in which: there is a piece of text 10 characters long program asks to choose number between 0-9 program returns the letter which is equal to the number chosen by More on stackoverflow.com
๐ŸŒ stackoverflow.com
regex - Python Parsing with a Variable Name and Square Brackets - Stack Overflow
Stack Data Licensing Data licensing offering for businesses to build and improve AI tools and models ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I want to the lines between the bracketed keywords to be processed. The variable ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 17, 2016
python - How to print a value from a variable in square brackets? - Stack Overflow
last= "smith" I am trying to print this line: John [Smith]. The answer is last = "Smith" msg = "John" + " ["+ last +"] " print(msg) But I don't More on stackoverflow.com
๐ŸŒ stackoverflow.com
Find elsewhere
Top answer
1 of 3
14

They are part of the Python syntax and unlike using single (') or double (") quotes, they can pretty much never be interchanged.

Square and rounded brackets can mean so many different things in different circumstances. Just to give an example, one may think that both the following are identical:

a = [1,2,3]
a = (1,2,3)

as a[0] gives 1 in both cases. However, the first one is creating a list whereas the second is a tuple. These are different data types and not knowing the distinction can lead to difficulties.

Above is just one example where square and rounded brackets differ but there are many, many others. For example, in an expression such as:

4 * ((12 + 6) / 9)

using square brackets would lead to a syntax error as Python would think you were trying to create a nested list:

4 * [[12 + 6] / 9]

So hopefully you can see from above, that the two types of brackets do completely different things in situations which seem identical. There is no real rule of thumb for when one type does what. In general, I guess that square brackets are used mainly for lists and indexing things whereas rounded brackets are for calculations (as you would in maths) and functions etc.

Hope this helps you out a bit!

2 of 3
7

It's hard to answer succinctly, but I can give you some common examples.

Square brackets define lists:

my_list = [1, 2, 3, 4]

They are also used for indexing lists. For instance:

print(my_list[1])

Returns 2. Additionally, they are frequently used to index dictionaries, which are defined with curly brackets:

my_dict = {5:'a', 6:'b', 7:'c'}

The indexing for dictionaries requires that I input the "key" as follows:

print(my_dict[6])

Returns b.

Functions are called using round brackets. For instance, if I want to add an element to my list, I can call the append() function:

my_list.append(8)

I have just added 8 to my list. You will notice that when I called the print function I also used curved brackets.

This is by no means comprehensive, but hopefully it will give a starting point.

Top answer
1 of 7
332

Square brackets are lists while parentheses are tuples.

A list is mutable, meaning you can change its contents:

>>> x = [1,2]
>>> x.append(3)
>>> x
[1, 2, 3]

while tuples are not:

>>> x = (1,2)
>>> x
(1, 2)
>>> x.append(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'

The other main difference is that a tuple is hashable, meaning that you can use it as a key to a dictionary, among other things. For example:

>>> x = (1,2)
>>> y = [1,2]
>>> z = {}
>>> z[x] = 3
>>> z
{(1, 2): 3}
>>> z[y] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Note that, as many people have pointed out, you can add tuples together. For example:

>>> x = (1,2)
>>> x += (3,)
>>> x
(1, 2, 3)

However, this does not mean tuples are mutable. In the example above, a new tuple is constructed by adding together the two tuples as arguments. The original tuple is not modified. To demonstrate this, consider the following:

>>> x = (1,2)
>>> y = x
>>> x += (3,)
>>> x
(1, 2, 3)
>>> y
(1, 2)

Whereas, if you were to construct this same example with a list, y would also be updated:

>>> x = [1, 2]
>>> y = x
>>> x += [3]
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
2 of 7
14

One interesting difference :

lst=[1]
print lst          // prints [1]
print type(lst)    // prints <type 'list'>

notATuple=(1)
print notATuple        // prints 1
print type(notATuple)  // prints <type 'int'>
                                         ^^ instead of tuple(expected)

A comma must be included in a tuple even if it contains only a single value. e.g. (1,) instead of (1).

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ question regarding using square brackets to access values of variables
r/learnpython on Reddit: Question regarding using square brackets to access values of variables
February 4, 2025 -

Hello,

Please review the code in bold.

My question is in both examples,

Example 1.

python_topics = ["variables", "control flow", "loops", "modules", "classes"]

length = len(python_topics)

index = 0

while index < length:

print("I am learning about " + python_topics[index])

index += 1

Example 2.

favorite_fruit = "blueberry"

last_char = favorite_fruit**[len(favorite_fruit) - 1]**

Output: Y

My question is, their a way to describe what the square brackets are doing? Would you say that the square brackets are used to access or transfer the value of the variable? Hopefully this question makes sense. Thank you very much.

๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-square-brackets-in-Python-except-list
What is the use of square brackets in Python, except list? - Quora
Answer (1 of 4): [] apart from being used to represent an empty list can also be used in the following ways: array indexing: [code]a = [1, 2, 3, 4, 5] # access first element print(a[0]) # access last element print(a[-1]) [/code]dictionary indexing: ...
๐ŸŒ
Medium
medium.com โ€บ generative-design โ€บ fundamentals-of-python-variables-b0523dd698a7
Fundamentals of Python โ€” Variables | by Danil Nagy | Generative Design | Medium
February 5, 2017 - To retrieve an object from such a list, you once again use square brackets, but this time appended to the end of the variable name. Inside the brackets you place the index or place of the piece of data you want.
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 2453-how-to-use-square-bracket-syntax-in-python
[Python] - How to Use Square Bracket Syntax in Python - | SheCodes
Learn what square bracket syntax is and how to use it to access elements of an array or dictionary in Python.
Top answer
1 of 1
1

But what is this syntax?

The first pair of square brackets is a list display. The second pair of square brackets is a slicing.

Does it always work?

"Always" is a very long time. There is no way of telling what Python will look like in a million years.

However, to my knowledge neither the syntax for list displays nor the syntax of slicings has ever changed in a backwards-incompatible manner in Python, at least in the simple, basic form in your code. It may have been extended, and some advanced forms may have been changed, but the basic form has always been the same.

In fact, the basic form of list displays and slicings in Python is not only the same across all versions of Python, but even many other programming languages as well. Using square brackets for lists / arrays is almost universal in languages that are inspired by ALGOL, as is using square brackets for indexing / subscripting / slicing.

Is it deprecated?

There is no mention of deprecating list displays or the current list display syntax, slicings or the current slicings syntax in the Deprecated section of the What's New In Python 3.10 document nor in the currently under development 3.11.

I also could not find any mention of deprecating list displays or the current list display syntax, slicings or the current slicings syntax in any of the Python Enhancement Proposals.

So, the earliest that they could be deprecated would be in Python 3.12, which means the earliest they could be removed would be in Python 3.13. However, that is vanishingly unlikely, since it would break every single Python program ever written. It would be an even more breaking change than the transition from Python 2 to Python 3, which took twelve years.

I googled it but cound't find any reference in the python docs or anywhere else

You can find the full syntax of Python in the chapter Full Grammar Specification of the Python Language Reference.

This is the syntax for list display:

list:
    | '[' [star_named_expressions] ']' 

and for slicing:

primary:
    | primary '.' NAME 
    | primary genexp 
    | primary '(' [arguments] ')' 
    | primary '[' slices ']' 
    | atom

slices:
    | slice !',' 
    | ','.slice+ [','] 
slice:
    | [expression] ':' [expression] [':' [expression] ] 
    | named_expression 
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 58891910 โ€บ what-is-in-this-squiggly-brackets
python - What is in this squiggly brackets? - Stack Overflow
it is a Python str.format() question Please see below- I cannot seem to understand the purpose of 2d in the {:2d} part... I see that : keeps the print output on the same output line... When I chan...