square_eyes
square_eyes

Reputation: 1271

Iterate over lists stored in dict values

I can't find anything that solves my problem.

I have a function testdata() that returns data slices as a dictionary. The keys are numbered as text (with a leading zero) for reference.

The function returns the below dict...

mydict = {}
some stuff here   
pprint(mydict)
{'01': [u'test1',
        u'test2',
        u'test3'],
 '02': [u'test4',
        u'test5',
        u'test6'],
 '03': [u'test7',
        u'test8',
        u'test9']
 }

I now want to send the slices's (01, 02, 03) key values, one by one to another function as a comma separated list/string.

So first iteration would be to access '01' and create the list 'test1,test2,test3' then send it as an argument to my other function analysis(arg).

Here's what I have...

getdata = testdata() # 
for x in getdata:
    incr = 0
    analysis(x['01': incr])
    incr += 1

I get the following error:

ERROR:root:Unexpected error:(<type 'exceptions.TypeError'>, TypeError('slice indices must be integers or None or have an __index__ method',), <traceback object at 0x10351af80>)

Upvotes: 0

Views: 427

Answers (4)

Brian Rodriguez
Brian Rodriguez

Reputation: 4359

keys = list(getdata) # Create a `list` containing the keys ('01', '02', ...)
sort(keys) # the order of elements from `list` is _unspecificed_, so we enforce an order here
for x in keys:
    css = ",".join(getdata[x]) # Now you have css = "test1,test2,test3"
    analysis(css) # dispatch and done

Or, even more succinctly (but with the same internal steps):

for x in sorted(getdata):
    analysis(",".join(getdata[x]))

As for your error, it's telling you that you can't use a string in slice notation. Slice notation is reserved for [lo:hi:step], and doesn't even work with dict anyway. The easiest way to "slice" a dict is through a dict comprehension.

Upvotes: 0

Chad S.
Chad S.

Reputation: 6633

Here's an example step by step of how this could be done..

gendata = {
 '01': [u'test1',u'test2',u'test3'],
 '02': [u'test4',u'test5',u'test6'],
 '03': [u'test7',u'test8',u'test9']
 }

#iterate over the keys sorted alphabetically 
#   (e.g. key=='01', then key=='02', etc)

for key in sorted(gendata):  
    value_list = gendata[key]  # e.g. value_list=['test1', 'test2, 'test3']
    joined_string = ','.join(value_list) # joins the value_list's items with commas
    analysis(joined_string) #calls the function with 'test1,test2,test3'

Upvotes: 0

Diogo Martins
Diogo Martins

Reputation: 937

analysis(x['01': incr]) when doing ['01': incr] you're using the list slice operator :, and is supposed to be used with integer indices. incr is an int, but '01' is a string.

If you only want to iterate of the dict values (the corresponding lists), its enough to do:

for key, the_list in mydict.iteritems():
    analysis(the_list)

Upvotes: 0

Aftnix
Aftnix

Reputation: 4589

    In [2]: dic
    Out[2]: 
    {'01': [u'test1', u'test2', u'test3'],
     '02': [u'test4', u'test5', u'test6'],
     '03': [u'test7', u'test8', u'test9']}

    In [6]: for k,v in dic.iteritems():
   ...:     print k,v
   ...:     
02 [u'test4', u'test5', u'test6']
03 [u'test7', u'test8', u'test9']
01 [u'test1', u'test2', u'test3']

So i guess you could just do a ..

analysis(k,v) 

Upvotes: 1

Related Questions