Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary, Sub element access

I have been looking into dictionary manipulation in python and I have reached an impasse on how to actually access sub elements of a generated dictionary.

(as you can maybe tell im pretty new to python coding)

d = {'Element': [1,2]}
print d['Element']

this returns:

[1,2]

But instead I would like to return only each element on separate lines.

Anyway if anyone has a better idea on how to store and access sub-elements it would be well appreciated to be shown the correct (or most correct) solution.

like image 764
Pariah Avatar asked Feb 03 '26 14:02

Pariah


2 Answers

As Fredrik Pihl noted:

d = {'element':[0,1]}
print d['element'][1]

Returns: 1

Thanks guys for your assistance.

like image 69
Pariah Avatar answered Feb 05 '26 04:02

Pariah


You can print line one by one with loop:

d = {'Element': [1,2]}

for line in d['Element']:
    print line
like image 33
matousc Avatar answered Feb 05 '26 04:02

matousc