[Tutor] pointers or references to variables or sub-sets of variables query.

Mats Wichmann mats at wichmann.us
Sun Jul 7 10:53:55 EDT 2019


On 7/6/19 8:39 PM, mhysnm1964 at gmail.com wrote:
> Hi all.
> 
> In C, you can use pointers to reference variables, arrays, ETC. In python, I
> do not recall anything specifically that refers to such a capability. What I
> want to do is:
> 
> I want to create different data structures such as dictionaries which
> contain specific  list elements based upon specific keys. The original data
> structure could look like:
> 
> Data = [
>   ['2019-01-19','Fred Flintstone',23],
>   ['2019-02-01','Scooby doo', 99]
> ]
> 
> The above structure does have 100's of elements. I want to reference
> specific lists within the above structure. Using the only method I know how:
> 
> Category = {'under-50':[data[0]], 'over-50':[data[1]]}
> 
> If I understand things correctly with Python. The above will copy the value
> into the list within the key. Not the memory address of the nested list I am
> referencing. I am using a list within the key to permit multiple references
> to different nested lists from the original data structure. The end result
> of the structure for the dict could look like this (using example, not real
> output)
> 
> Category['under-50'] = [ List1 pointer, List22 pointer, List52 pointer]
> 
> I hope the above makes sense. How can this be done? 

It's easy enough to convince yourself that what Alan said is true. You
can, for example, use the id function to show this:

https://docs.python.org/3/library/functions.html#id

# identity of 0'th element of Data:
print(id(Data[0]))
# identity of the list that is the value of 'under-50' key:
print(id(Category['under-50']))
# identity of 0'th element of that list:
print(id(Category['under-50'][0])

the first and third should be the same, showing you that's the same
object referred to by those two places.  Again, like Python's
"variables", these are just references to objects. As in:

item = Data[0]
print(id(item), id(Data[0]))

(note: id() is handy for explorations, especially interactive ones, but
isn't terribly useful for production code.  don't attach any meaning to
the value returned by id() other than "unique" - different Pythons
famously generate different id values, something that's been known to
confuse people doing experiments in, for example, PyPy)

Since you turned up here you sometimes also get free unasked-for advice:
I know this was a toy fragment just to explain the concept you're
getting at, but you'll normally want to build your design in a way that
minimizes "magic".  Using numbered indices into an array-like structure
is one of those bits of magic that raises flags. To refer to Fred's age,
you could end up with  Data[0][2].  That would be pretty ugly, and
worse, hard to remember what it meant.  Try to seek ways you can give
meaningful names to things.  We can make suggestions if that's of
interest (I don't want to belabor the point).


More information about the Tutor mailing list