Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through dictionary values and print subsequently

I am trying to print the following dictionary in a hierarchy format

fam_dict{'6081740103':['60817401030000','60817401030100','60817401030200',
'60817401030300','60817401030400','60817401030500','60817401030600'] 

as shown here:

60817401030000
    60817401030100
        60817401030200
            60817401030400
                60817401030500
                    60817401030600

So far I have the following code which works but I'm having to manually input the i'th index in each line. How can I readjust this code in a recursive format instead of having to count how many lines of code and manually put the index value each time

  my_p = node(fam_dict['6081740103'][0], None)
    my_c = node(fam_dict['6081740103'][1], my_p)
    my_d = node(fam_dict['6081740103'][2], my_c)
    my_e = node(fam_dict['6081740103'][4], my_d)
    my_f = node(fam_dict['6081740103'][5], my_e)
    my_g = node(fam_dict['6081740103'][6], my_f)

    print (my_p.name)
    print_children(my_p)
like image 392
nm6834 Avatar asked Sep 24 '26 02:09

nm6834


1 Answers

You can try this:

fam_dict = {'6081740103':['60817401030000','60817401030100','60817401030200',
'60817401030300','60817401030400','60817401030500','60817401030600']}

for i, val in enumerate(fam_dict['6081740103']):
    print(' ' * i * 4 + val)

Which outputs your desired hierachy:

60817401030000
    60817401030100
        60817401030200
            60817401030300
                60817401030400
                    60817401030500
                        60817401030600
like image 158
RoadRunner Avatar answered Sep 25 '26 19:09

RoadRunner