python: How come list element lookup is O(1)
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Today in class, we learned that retrieving an element from a list is O(1)
in Python. Why is this the case? Suppose I have a list of 4 items ie:
li = ["perry", 1, 23.5, "s"]
These items have different sizes in memory. And so it is not possible to take the memory location of li[0]
and add 3 times the size of each element to get the memory location of li[3]
. So how does the interpreter know where li[3]
is without having to traverse the list in order to retrieve the element?
python arrays list
add a comment |Â
up vote
6
down vote
favorite
Today in class, we learned that retrieving an element from a list is O(1)
in Python. Why is this the case? Suppose I have a list of 4 items ie:
li = ["perry", 1, 23.5, "s"]
These items have different sizes in memory. And so it is not possible to take the memory location of li[0]
and add 3 times the size of each element to get the memory location of li[3]
. So how does the interpreter know where li[3]
is without having to traverse the list in order to retrieve the element?
python arrays list
6
What makes you think that arrays are linearly allocated rather than a list of pointers. - [me confused by your profile description]
â Morrison Chang
3 hours ago
9
Don't confuse item access, which isO(1)
, with item lookup / search, which isO(n)
.
â Mike Scotty
3 hours ago
1
Lists don't physically contain other objects. It's almost impossible to have one object physically contain another object in Python.
â user2357112
3 hours ago
2
Some relevant reading material: Python list implementation, How is Python's List Implemented?, What is the underlying data structure for Python lists?.
â Warren Weckesser
3 hours ago
Pythonlist
objects are implemented as array-lists, not a linked-list structure. They are essentially arrays of PyObject pointers, in CPython.
â juanpa.arrivillaga
2 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Today in class, we learned that retrieving an element from a list is O(1)
in Python. Why is this the case? Suppose I have a list of 4 items ie:
li = ["perry", 1, 23.5, "s"]
These items have different sizes in memory. And so it is not possible to take the memory location of li[0]
and add 3 times the size of each element to get the memory location of li[3]
. So how does the interpreter know where li[3]
is without having to traverse the list in order to retrieve the element?
python arrays list
Today in class, we learned that retrieving an element from a list is O(1)
in Python. Why is this the case? Suppose I have a list of 4 items ie:
li = ["perry", 1, 23.5, "s"]
These items have different sizes in memory. And so it is not possible to take the memory location of li[0]
and add 3 times the size of each element to get the memory location of li[3]
. So how does the interpreter know where li[3]
is without having to traverse the list in order to retrieve the element?
python arrays list
python arrays list
edited 27 mins ago
asked 3 hours ago
Teererai Marange
62621331
62621331
6
What makes you think that arrays are linearly allocated rather than a list of pointers. - [me confused by your profile description]
â Morrison Chang
3 hours ago
9
Don't confuse item access, which isO(1)
, with item lookup / search, which isO(n)
.
â Mike Scotty
3 hours ago
1
Lists don't physically contain other objects. It's almost impossible to have one object physically contain another object in Python.
â user2357112
3 hours ago
2
Some relevant reading material: Python list implementation, How is Python's List Implemented?, What is the underlying data structure for Python lists?.
â Warren Weckesser
3 hours ago
Pythonlist
objects are implemented as array-lists, not a linked-list structure. They are essentially arrays of PyObject pointers, in CPython.
â juanpa.arrivillaga
2 hours ago
add a comment |Â
6
What makes you think that arrays are linearly allocated rather than a list of pointers. - [me confused by your profile description]
â Morrison Chang
3 hours ago
9
Don't confuse item access, which isO(1)
, with item lookup / search, which isO(n)
.
â Mike Scotty
3 hours ago
1
Lists don't physically contain other objects. It's almost impossible to have one object physically contain another object in Python.
â user2357112
3 hours ago
2
Some relevant reading material: Python list implementation, How is Python's List Implemented?, What is the underlying data structure for Python lists?.
â Warren Weckesser
3 hours ago
Pythonlist
objects are implemented as array-lists, not a linked-list structure. They are essentially arrays of PyObject pointers, in CPython.
â juanpa.arrivillaga
2 hours ago
6
6
What makes you think that arrays are linearly allocated rather than a list of pointers. - [me confused by your profile description]
â Morrison Chang
3 hours ago
What makes you think that arrays are linearly allocated rather than a list of pointers. - [me confused by your profile description]
â Morrison Chang
3 hours ago
9
9
Don't confuse item access, which is
O(1)
, with item lookup / search, which is O(n)
.â Mike Scotty
3 hours ago
Don't confuse item access, which is
O(1)
, with item lookup / search, which is O(n)
.â Mike Scotty
3 hours ago
1
1
Lists don't physically contain other objects. It's almost impossible to have one object physically contain another object in Python.
â user2357112
3 hours ago
Lists don't physically contain other objects. It's almost impossible to have one object physically contain another object in Python.
â user2357112
3 hours ago
2
2
Some relevant reading material: Python list implementation, How is Python's List Implemented?, What is the underlying data structure for Python lists?.
â Warren Weckesser
3 hours ago
Some relevant reading material: Python list implementation, How is Python's List Implemented?, What is the underlying data structure for Python lists?.
â Warren Weckesser
3 hours ago
Python
list
objects are implemented as array-lists, not a linked-list structure. They are essentially arrays of PyObject pointers, in CPython.â juanpa.arrivillaga
2 hours ago
Python
list
objects are implemented as array-lists, not a linked-list structure. They are essentially arrays of PyObject pointers, in CPython.â juanpa.arrivillaga
2 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
A list in Python is implemented as an array of pointers1. So, what's really happening when you create the list:
["perry", 1, 23.5, "s"]
is that you are actually creating an array of pointers like so:
[0xa3d25342, 0x635423fa, 0xff243546, 0x2545fade]
Each pointer "points" to the respective objects in memory, so that the string "perry"
will be stored at address 0xa3d25342
and the number 1
will be stored at 0x635423fa
, etc.
Since all pointers are the same size, the interpreter can in fact add 3 times the size of an element to the address of li[0]
to get to the pointer stored at li[3]
.
1 Get more details from: the horse's mouth (CPython source code on GitHub).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
A list in Python is implemented as an array of pointers1. So, what's really happening when you create the list:
["perry", 1, 23.5, "s"]
is that you are actually creating an array of pointers like so:
[0xa3d25342, 0x635423fa, 0xff243546, 0x2545fade]
Each pointer "points" to the respective objects in memory, so that the string "perry"
will be stored at address 0xa3d25342
and the number 1
will be stored at 0x635423fa
, etc.
Since all pointers are the same size, the interpreter can in fact add 3 times the size of an element to the address of li[0]
to get to the pointer stored at li[3]
.
1 Get more details from: the horse's mouth (CPython source code on GitHub).
add a comment |Â
up vote
9
down vote
accepted
A list in Python is implemented as an array of pointers1. So, what's really happening when you create the list:
["perry", 1, 23.5, "s"]
is that you are actually creating an array of pointers like so:
[0xa3d25342, 0x635423fa, 0xff243546, 0x2545fade]
Each pointer "points" to the respective objects in memory, so that the string "perry"
will be stored at address 0xa3d25342
and the number 1
will be stored at 0x635423fa
, etc.
Since all pointers are the same size, the interpreter can in fact add 3 times the size of an element to the address of li[0]
to get to the pointer stored at li[3]
.
1 Get more details from: the horse's mouth (CPython source code on GitHub).
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
A list in Python is implemented as an array of pointers1. So, what's really happening when you create the list:
["perry", 1, 23.5, "s"]
is that you are actually creating an array of pointers like so:
[0xa3d25342, 0x635423fa, 0xff243546, 0x2545fade]
Each pointer "points" to the respective objects in memory, so that the string "perry"
will be stored at address 0xa3d25342
and the number 1
will be stored at 0x635423fa
, etc.
Since all pointers are the same size, the interpreter can in fact add 3 times the size of an element to the address of li[0]
to get to the pointer stored at li[3]
.
1 Get more details from: the horse's mouth (CPython source code on GitHub).
A list in Python is implemented as an array of pointers1. So, what's really happening when you create the list:
["perry", 1, 23.5, "s"]
is that you are actually creating an array of pointers like so:
[0xa3d25342, 0x635423fa, 0xff243546, 0x2545fade]
Each pointer "points" to the respective objects in memory, so that the string "perry"
will be stored at address 0xa3d25342
and the number 1
will be stored at 0x635423fa
, etc.
Since all pointers are the same size, the interpreter can in fact add 3 times the size of an element to the address of li[0]
to get to the pointer stored at li[3]
.
1 Get more details from: the horse's mouth (CPython source code on GitHub).
edited 2 hours ago
answered 3 hours ago
DJG
3,84232040
3,84232040
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52684993%2fpython-how-come-list-element-lookup-is-o1%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
6
What makes you think that arrays are linearly allocated rather than a list of pointers. - [me confused by your profile description]
â Morrison Chang
3 hours ago
9
Don't confuse item access, which is
O(1)
, with item lookup / search, which isO(n)
.â Mike Scotty
3 hours ago
1
Lists don't physically contain other objects. It's almost impossible to have one object physically contain another object in Python.
â user2357112
3 hours ago
2
Some relevant reading material: Python list implementation, How is Python's List Implemented?, What is the underlying data structure for Python lists?.
â Warren Weckesser
3 hours ago
Python
list
objects are implemented as array-lists, not a linked-list structure. They are essentially arrays of PyObject pointers, in CPython.â juanpa.arrivillaga
2 hours ago