User Tools

Site Tools


python:python-code

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:python-code [14/02/2025 10:40] – [CLI input selection for an option] andrewpython:python-code [14/02/2025 10:48] (current) – [Print both keys and values] andrew
Line 23: Line 23:
  
  
-==== Print all the values ====+==== Print all the values - version 1 ====
  
 +<code python>
 +exampledict = {
 +  "class": "Modified Hall",
 +  "engine": "Witherslack Hall",
 +  "built": "1948"
 +}
 +for x in exampledict:
 +  print(exampledict[x])
 +</code>
 +
 +<code>
 +Modified Hall
 +Witherslack Hall
 +1948
 +</code>
 +
 +
 +==== Print all the values - version 2 ====
 +
 +This uses the Python ".values()" function to get the values of each dictionary item.
 +<code python>
 +exampledict = {
 +  "class": "Modified Hall",
 +  "engine": "Witherslack Hall",
 +  "built": "1948"
 +}
 +for x in exampledict.values():
 +  print(x)
 +</code>
 +
 +<code>
 +Modified Hall
 +Witherslack Hall
 +1948
 +</code>
  
  
 ==== Print both keys and values ==== ==== Print both keys and values ====
  
 +Using the "items()" function to return both the value and the key for wach item.
 +
 +<code python>
 +exampledict = {
 +  "class": "Modified Hall",
 +  "engine": "Witherslack Hall",
 +  "built": "1948"
 +}
 +for x,y in exampledict.items():
 +  print(x, y)
 +</code>
  
 +<code>
 +class Modified Hall
 +engine Witherslack Hall
 +built 1948
 +</code>
  
  
python/python-code.1739529638.txt.gz · Last modified: by andrew

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki