php vs python

Brian Quinlan brian at sweetapp.com
Sat Jan 4 20:20:04 EST 2003


> How do I loop over an associative array (dictionary) with easy access
> to key and value variables on each iteration. In PHP I do this :
> 
>   foreach($table as $key => $value) {
>     echo $key.'='.$value;
>   }

Here are two ways:

for key, value in table.items():
	print key, '=', value
	print '%s=%s' % (key, value) # i like formatted printing

for key in table:
	print key, '=', table[key]

Cheers,
Brian






More information about the Python-list mailing list