Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does a Python list hold its values?

Where are the list values located in the class object?

If a list object is a class in python:

>>> a = ['one', 'two']
>>> type(a)
<class 'list'>

So it is stored somewhere in the class, but where?

For example:

If we define a class with values:

class Test:
    def __init__(self):
        self.test_name = "Do Not"
        self.test_surname = "Know"

It is easy to locate an instance values:

>>> b = Test()
>>> print(b.__dict__)
{'test_surname': 'Know', 'test_name': 'Do not'}

Is there similar option to reach those values in the list class object?

like image 311
Vy.Iv Avatar asked May 28 '16 00:05

Vy.Iv


People also ask

How is data stored in a list Python?

Definition: A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets.

Do Python list store values or pointers?

Do python lists store values or pointers? Python lists don't store values themselves. They store pointers to values stored elsewhere in memory. This allows lists to be mutable.

What can a list hold in Python?

A list can have any number of elements. They are similar to arrays in other programming languages. Lists can hold all kinds of variables: integers (whole numbers), floats, characters, texts and many more.

How do you find the place of a value in a list Python?

To find a position of the particular element you can use the index() method of List class with the element passed as an argument. An index() function returns an integer (position) of the first match of the specified element in the List.


1 Answers

This is really up to the implementation detail. In cpython, the container object only hold references (pointers) to the stored values. Any operation involving the list internals only manipulates the pointers, not the objects.

Memory is over-allocated so that there are always some free slots available, which makes appends and inserts faster. The space allocated increased by about 12.5% when full. You can actually see that yourself by appending to a list and calling sys.getsizeof in a loop:

>>> import sys
>>> l = []
>>> for i in range(100):
...     print(sys.getsizeof(l)),
...     l.append(None)
...     
72 104 104 104 104 136 136 136 136 200 200 200 200 200 200 200 200 272 272 272 272 272 272 272 272 272 352 352 352 352 352 352 352 352 352 352 440 440 440 440 440 440 440 440 440 440 440 536 536 536 536 536 536 536 536 536 536 536 536 648 648 648 648 648 648 648 648 648 648 648 648 648 648 776 776 776 776 776 776 776 776 776 776 776 776 776 776 776 776 920 920 920 920 920 920 920 920 920 920 920

You can not find a dict of the items behind the scenes somewhere, like you have done with the attributes. The list itself is merely an array storing it's length and the memory locations of the items.

enter image description here

image source: here

like image 191
wim Avatar answered Oct 12 '22 17:10

wim