[Tutor] dictionary values in strings

Bernard Lebel 3dbernard at gmail.com
Thu May 19 22:33:20 CEST 2005


Indeed, dictionaries don't have a .key attribute.

Instead, use:

# Get list of values for 'key1'
aList = dol[ 'key1' ]


This would return the list of values you have assigned to 'key1' in
the dictionary. Once you got that list, you can look in the list to
find out the index of 'lil1' and return it:



# Iterate list of values assigned to 'key1'
for i in range( 0, len( dol[ 'key1' ] ) ):
	
	# Get list element using current iteration value
	sValue = dol[ 'key1' ][i]
	
	# If list element is 'lil1', print its list index
	if sValue == 'lil1': print 'lil1 has index : ' + str( i )


Cheers
Bernard


On 5/19/05, William O'Higgins <william.ohiggins at utoronto.ca> wrote:
> I am trying to discover the syntax for call on a dictionary of lists by
> key and index.
> 
> The data structure looks like this:
> 
> dol = {'key1':['li1','li2','li3'],'key2':['li1','li2','li3'],\
> 'key3':['li1'li2,'li3','']}
> 
> The keys are passed to a function as arguments, and I want the value of
> the specified list index.  This is what I *thought* it would look like:
> 
> dol.key(argument)[0] # would return li1 when argument equals key1
> 
> But that's wrong.  The error I get is this:
> AttributeError: 'dict' object has no attribute 'key'
> 
> I don't know how to interpret that error (well, I know I screwed up, but
> ... :-)  Thanks.
> --
> 
> yours,
> 
> William
> 
> 
> 
> BodyID:3456163.2.n.logpart (stored separately)
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
>


More information about the Tutor mailing list