<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Dictionaries</id>
	<title>Dictionaries - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Dictionaries"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Dictionaries&amp;action=history"/>
	<updated>2026-05-14T01:24:40Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=Dictionaries&amp;diff=9198&amp;oldid=prev</id>
		<title>Kristian Rother at 19:58, 5 February 2013</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Dictionaries&amp;diff=9198&amp;oldid=prev"/>
		<updated>2013-02-05T19:58:16Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Cat|Python Commands}}&lt;br /&gt;
{{Python Links}}&lt;br /&gt;
&lt;br /&gt;
== Creating dictionaries ==&lt;br /&gt;
Dictionaries are an unordered, associative array.&lt;br /&gt;
They have a set of key/value pairs. They are very versatile data structures, but slower than lists.&lt;br /&gt;
Dictionaries can be used easily as a hashtable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
prices = {&lt;br /&gt;
&amp;#039;banana&amp;#039;:0.75,&lt;br /&gt;
&amp;#039;apple&amp;#039;:0.55,&lt;br /&gt;
&amp;#039;orange&amp;#039;:0.80&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing elements in dictionaries ==&lt;br /&gt;
By applying square brackets with a key inside, the values of a dictionary can be requested. Valid types for keys are strings, integers, floats, and tuples.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print prices[&amp;#039;banana&amp;#039;]&lt;br /&gt;
print prices[&amp;#039;kiwi&amp;#039;]&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Looping over a dictionary ==&lt;br /&gt;
You can access the keys of a dictionary in a for loop. However, their order is not guaranteed, then.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for fruit in prices:&lt;br /&gt;
print fruit&lt;br /&gt;
&lt;br /&gt;
# 0.75&lt;br /&gt;
# KeyError!&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Dictionary functions ==&lt;br /&gt;
There is a number of functions that can be used on every dictionary:&lt;br /&gt;
&lt;br /&gt;
===== Checking whether a key exists: =====&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
prices.has_key(&amp;#039;apple&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Retrieving values in a fail-safe way: =====&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
prices.get(&amp;#039;banana&amp;#039;) # 0.75&lt;br /&gt;
&lt;br /&gt;
prices.get(&amp;#039;kiwi&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Setting values if they dont exist yet: =====&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
prices.setdefault(&amp;#039;kiwi&amp;#039;,0.99)&lt;br /&gt;
&lt;br /&gt;
prices.setdefault(&amp;#039;banana&amp;#039;,0.99)&lt;br /&gt;
&lt;br /&gt;
# for &amp;#039;banana&amp;#039;, nothing happens.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Getting all keys / values: =====&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print prices.keys()&lt;br /&gt;
&lt;br /&gt;
print prices.values()&lt;br /&gt;
&lt;br /&gt;
# None&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kristian Rother</name></author>
	</entry>
</feed>