Square brackets or rounded brackets?

When dealing with lists if I want to assign a new variable for a specific element in a list I do this

new_variable = listname[3]

If I want to remove an item from a list I do


Is there a reason why we use square brackets for the first task and parentheses for the second? In both we’re specifying the element we want to do something with.
The answer is probably straightforward and obvious but I’m fairly new to this and I wondered if there was an easy to remember rule so I know which to use.

Thanks in advance


Parenthesis are used for the parameters of a function/method.


Brackets (in python) are used to either reference the index or the key of a data structure like a list or dictionary

box[4] # returns the item at index-4 of the box list.
people_i_know["michael"] # returns the value 
# for the key "michael" in the
# dictionary people_i_know

This works for some other data structures like tuples as well.


Thank you.

In my example listname.pop(3) I’m telling python to remove the item in the list which has the index of 3 though aren’t I?

Correct, this is a method that list has. A method is kind of like a function but inside of a class/object. So in this case your list is that object, and imagine inside it it has some def pop(self, value) function defined. So when you write some_list.pop(3) you are calling that function that is pre-defined in the list.

Thank you.
So a good way of remembering it would be if I’m doing anything with list.pop, list.insert etc its always parentheses and if I’m just referencing a specific place in a list any other time then it’s square brackets?

Yes, in either case you will use it so often that it’ll become ingrained rather quickly. The conventions are incredibly similar across the programming languages so once you have the concept it should be smooth(er) sailing.

1 Like

Great, thank you. Yes I had a feeling in not so long when all this is more familiar it’ll be something I never have to think about.

Thank you for your help.