CatalunaLilith
CatalunaLilith

Reputation: 21

Python, how to pass reference to list of list (not value)

I want to pass a refference to a list of lists of ints, NOT the int value itself.

I have a list of list of ints, which represents a root-first binary tree, which I am calling aTriangle I am building a list called path as I am walking down aTriangle path is being filled with references to aTriangle of the format aTriangle[i][j] the problem is that when I try to print or pass path, it is a list of int values, NOT a list of the references that I put in it.

For example,

test_triangle = [[1], [2, 3], [4, 5, 6]]
test_list = [test_triangle[0][0], test_triangle[1][0]]
print(test_list)

prints out [1, 2] (similarly, passing test_list passes on the values)

but I want [test_triangle[0][0], test_triangle[1][0]]

How do I build a list of references that STAY as references? Or if this is infeasible, if there another method for me to keep tract of both indexes associated with the value, since these index values are important to later steps.

Upvotes: 1

Views: 137

Answers (1)

gsb22
gsb22

Reputation: 2180

Are you looking for something like this?

Disclaimer -> This is pure hack, I would suggest to look for more sophisticated python module/library.

test_triangle = [[1], [2, 3], [4, 5, 6]]
test_list = (test_triangle[0][0], test_triangle[1][0])

values_and_indexes = {}

for index, value in enumerate(test_triangle):
    for _i, _v in enumerate(value):
        values_and_indexes[f"test_triangle[{index}][{_i}]"] = _v

print(values_and_indexes)

OUTPUT

{'test_triangle[0][0]': 1, 'test_triangle[1][0]': 2, 'test_triangle[1][1]': 3, 'test_triangle[2][0]': 4, 'test_triangle[2][1]': 5, 'test_triangle[2][2]': 6}

Upvotes: 1

Related Questions