{article Dive into Python}{title} {text} {/article}
Iterating Through a Dictionary
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> for k, v in os.environ.items():
print ("%s=%s" % (k, v))
SSMA_M2SS_EP_HOME=C:\Microsoft SQL Server Migration Assistant for MySql Extension Pack\
PROGRAMW6432=C:\Program Files
USERDOMAIN=Tom-GatewayPC
TCL_LIBRARY=C:\Python34\tcl\tcl8.6
PROGRAMDATA=C:\ProgramData
FP_NO_HOST_CHECK=NO
DFSTRACINGON=FALSE
USERNAME=Tom
SYSTEMDRIVE=C:
TK_LIBRARY=C:\Python34\tcl\tk8.6
#..........................................#
>>> print ("\n".join(["%s=%s" % (k, v)
for k, v in os.environ.items()]))
SSMA_M2SS_EP_HOME=C:\Microsoft SQL Server Migration Assistant for MySql Extension Pack\
PROGRAMW6432=C:\Program Files
USERDOMAIN=Tom-GatewayPC
TCL_LIBRARY=C:\Python34\tcl\tcl8.6
PROGRAMDATA=C:\ProgramData
FP_NO_HOST_CHECK=NO
DFSTRACINGON=FALSE
USERNAME=Tom
SYSTEMDRIVE=C:
TK_LIBRARY=C:\Python34\tcl\tk8.6
COMMONPROGRAMFILES=C:\Program Files\Common Files
HOMEDRIVE=C:
VS120COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\
#................................................#
os.environ is a dictionary of the environment variables defined on your system. In Windows, these are your
user and system variables accessible from MS−DOS. In UNIX, they are the variables exported in your shell's
startup scripts. In Mac OS, there is no concept of environment variables, so this dictionary is empty.
os.environ.items() returns a list of tuples: [(key1, value1), (key2, value2), ...]. The for loop iterates through this list. The first round, it assigns key1 to k and value1 to v, so k = USERPROFILE and v = C:\Documents and Settings\mpilgrim. In the second round, k gets the second key, OS, and v gets the corresponding value, Windows_NT.
With multi−variable assignment and list comprehensions, you can replace the entire for loop with a single
statement. Whether you actually do this in real code is a matter of personal coding style. I like it because it makes it clear that what I'm doing is mapping a dictionary into a list, then joining the list into a single string. Other programmers prefer to write this out as a for loop. The output is the same in either case, although this version is slightly faster, because there is only one print statement instead of many.
{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> for k, v in os.environ.items():
print ("%s=%s" % (k, v))
SSMA_M2SS_EP_HOME=C:\Microsoft SQL Server Migration Assistant for MySql Extension Pack\
PROGRAMW6432=C:\Program Files
USERDOMAIN=Tom-GatewayPC
TCL_LIBRARY=C:\Python34\tcl\tcl8.6
PROGRAMDATA=C:\ProgramData
FP_NO_HOST_CHECK=NO
DFSTRACINGON=FALSE
USERNAME=Tom
SYSTEMDRIVE=C:
TK_LIBRARY=C:\Python34\tcl\tk8.6
#..........................................#
>>> print ("\n".join(["%s=%s" % (k, v)
for k, v in os.environ.items()]))
SSMA_M2SS_EP_HOME=C:\Microsoft SQL Server Migration Assistant for MySql Extension Pack\
PROGRAMW6432=C:\Program Files
USERDOMAIN=Tom-GatewayPC
TCL_LIBRARY=C:\Python34\tcl\tcl8.6
PROGRAMDATA=C:\ProgramData
FP_NO_HOST_CHECK=NO
DFSTRACINGON=FALSE
USERNAME=Tom
SYSTEMDRIVE=C:
TK_LIBRARY=C:\Python34\tcl\tk8.6
COMMONPROGRAMFILES=C:\Program Files\Common Files
HOMEDRIVE=C:
VS120COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\
#................................................#
</pre>
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
// You can place PHP like this
?>
{/source}