0

Although I can access the list and see the elements of the list. I am unable to access the individual elements of the list. Upon trying to access any element, I am getting "list index out of range" error

            Person_CUID=[]
            Family_ID=[]
            Age=[]
            for key in df_demographics.keys():
                for k,v in df_demographics[key].items():
                    #print("Actual Key:%s" %key)
                    #print("key:%s"%k)
                    #print("Value:%s"%v)
                    if key.find('Family_ID')>=0 and k>0:
                        Family_ID.append(v)
                    elif key.find('Client Unique ID (CUID)')>=0 and k>0:
                        Person_CUID.append(v)
                    elif key.find('Age')>=0 and k>0:
                        Age.append(v)
                    elif key.find(key)>=0 and key.find('Metric Name')<0 and key.find('CBO_ID')<0 and key.find('Family_ID')<0 and k==0:
                        Metric1=v
                    elif key.find(key)>=0 and key.find('Metric Name')<0 and key.find('CBO_ID')<0 and key.find('Family_ID')<0 and k>=1:
                        Metric1_value=v
                        #for k,v in enumerate(Age):
                            #   print(k,v)
                        #for i in range(len(Age)):
                            #print(Age[i])
                        print(Age[k-1)
                    else:
                        pass```
I am able to access the elements separately if I use another `for x in range loop` but that would make my elements repeat
RB17
  • 356
  • 1
  • 8
  • 26
  • Can you provide a small test example with your expected output and explanation? Considering there is a lot of looping , may be a better way exists. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – anky May 03 '20 at 08:07
  • Look into the last elif block, if I uncomment the lines, where I am printing the list. I will get the output as: ``` 0 14 1 38 2 39 3 63 ``` If I uncomment the portion where I am printing the elements of the list in for loop, I get: ``` 14 38 39 63 ``` But if I try calling each individual element the way it is there in the code right now, I am getting Index out of range error – RB17 May 03 '20 at 12:50

0 Answers0