0

I have list of elements like a=[0,1,2,3], I want to convert these list elements into multidimensional dictionary a[0][1][2][3] is it possible using python.

i.e input a=[0,1,2,3] and desired output a[0][1][2][3]

I have a function that call the a[0] if list contains a=[0], a[0][1] if list contains a=[0,1] and a[0][1][2] if list contains a=[0,1,2] elements. I have elements of the list but not able to call my function according to my need.

I don't want to use numpy package, I there any easy method?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user199354
  • 505
  • 1
  • 5
  • 17
  • I don't think the problem is well defined. What's the value of a[0][1][2][3]? None? – Korem Aug 20 '14 at 06:10
  • 1
    Do you want to fill a dict or just access an item in a nested dict given a sequence of keys? If the latter, take a look at [this](http://stackoverflow.com/q/14692690/1258041). – Lev Levitsky Aug 20 '14 at 06:10
  • I've marked this as a duplicate; if the linked question does not address your problem, then please clarify the difference by editing the question, and it will be reopened. – Lev Levitsky Aug 20 '14 at 06:18
  • For setting values in a dict, see http://stackoverflow.com/q/13687924/1258041 – Lev Levitsky Aug 20 '14 at 06:22
  • I have a function which call a[0] then after some time it call a[0][1] and a[0][1][2] depending upon the list element a=[0],a=[0,1] and a=[0,1,2]. – user199354 Aug 20 '14 at 06:30
  • @LevLevitsky the solution that your are offering is totally different from my problem and this question is not duplicate, kindly check my problem again. – user199354 Aug 20 '14 at 06:37
  • @user199354 try this call: nested_set(d, [0,1,2,3], {}) . Using the function of the linked question you can access like you want d[0][1][2][3] – xecgr Aug 20 '14 at 06:44
  • Please [edit] the question by clarifying exactly what you need. From your question and comments, it's hard to understand what you already have and what you are trying to achieve. – Lev Levitsky Aug 20 '14 at 06:45
  • @LevLevitsky I have edit the question hopefully I make better sense now. – user199354 Aug 20 '14 at 06:50
  • Maybe include a specific example? I still don't see the difference. – Lev Levitsky Aug 20 '14 at 06:54

0 Answers0