prestotron prestotron - 1 year ago 91
Perl Question

Python: data Structures inside another

In Perl you can do stuff like this:

my $data->[texts}->{text1} = "hey";
print data->{texts}->{text1};

and it will print "hey". It is like a data structure (sort of array) inside another data structure...

Obviously this is possible in Python:

data = { 'jack': 4098, 'sape': 4139 }
print data['jack'];

But I want something like: data['texts']['text1'] like it was done in Perl.

And I need to be able to easily remove and add to this structure...


Answer Source

You are using dict object here. It can store any type of elements you want, including another dict object.

That means, you can initialize your data like:

data = {'jack': {'age': 20, 
                 'gender': 'M'

        'sape': {'age': 35, 
                 'gender': 'F'

And then refer to it's internal values:

print(data['jack']['age'] # prints "20"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download