2

I am new to data structures in python and was wondering how do you simulate a thing like pointers in python so that multiple structures can refer and manage the same piece of data.

I have the following two structures

my_list = [1]
my_dictionary = {}
my_dictionary["hello"] = my_list[0]

and when I do the following I get True

 id(my_dictionary["hello"]) == my_list[0]

However how can I force removal both from the dict and the list in one go? If I do the following my_dictionary still has a reference to my_list[0] i.e. 1

del my_list[0]

is there a way to get rid of both of these elements in one go? What is the python way of doing linked structures like this?

Har
  • 3,727
  • 10
  • 41
  • 75
  • `del my_list[0];del my_dictionary["hello"]`? – KSFT Jan 22 '16 at 23:34
  • but that means that they are not linked hence duplicating the effort...what if I forgot to delete the element from one structure but not the other? seems error prone – Har Jan 22 '16 at 23:36
  • You aren't going to be able to delete a key from a dictionary by removing an element from a list (without doing very complicated stuff). They're two different objects, and calling a method of one won't change the other. – KSFT Jan 22 '16 at 23:39
  • "However how can I force removal both from the dict and the list in one go?" - pointers wouldn't have done that for you either. – user2357112 Jan 22 '16 at 23:49
  • `my_dictionary["hello"]` does not have a reference to `my_list[0]`. Both have a reference to an integer object representing 1. `del my_list[0]` just decrements this reference counter by 1 – shmee Jan 22 '16 at 23:49

4 Answers4

2

It really depends on the point you're trying to solve by cross-referencing.


Suppose your intent is to be able to efficiently both locate an item by key, as well as to sequentially iterate by order. In this case, irrespective of the language, you probably would wish to avoid cross referencing a hash-table and an array data structures, as the updates are inherently linear. Conversely, cross referencing a hash-table and a list might make more sense.

For this, you can use something like llist:

 d = {}
 l = llist.dllist()

 # insert 'foo' and obtain the link
 lnk = l.append('foo')
 # insert the link to the dictionary
 d['foo'] = lnk

Conversely, suppose your intent is to be able to efficiently both locate an item by key, as well as to locate by index. Then you can use a dict and a list, and rebuild the list on each modification of the dict. There is no real reason for fancy cross-referencing.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
0

Simply put, there is no way to easily link your two structures.

You could manipulate the object you point to so that it has some "deleted" state and would act as if it's deleted (while being in both containers).

However, if all you wanted was a list from a dict, use list(the_dict.values()).

You could make a class to achieve this, if all else fails. See https://docs.python.org/2/reference/datamodel.html#emulating-container-types for the details on what your class would have to have. Within the class, you would have your "duplicated effort," but if it's correctly implemented it wouldn't be error prone.

Heman Gandhi
  • 1,343
  • 9
  • 12
-1

You can always do things like this:

Pointers in Python?

(a quick stackoverflow search shows some results)

But that is messing with more than just data structures. Remember that Python manages memory for you (in most of the cases, pretty well), so you don't have to worry of cleaning after yourself.

Community
  • 1
  • 1
Wakaru44
  • 423
  • 4
  • 14
-1

i have tried the following peice of code and it works (changing in one DataStructure changes for the other). does this help?

list1 = [1,2,3]
list2 = [4,5,6]

my_dictionary = {}
my_dictionary["a"] = list1
my_dictionary["b"] = list2

del list1[0]
print list1
print list2
print my_dictionary
Samer Aamar
  • 1,298
  • 1
  • 15
  • 23