hi there Serdar Yegulalp from InfoWorld at IDG today I'll be talking
about the power of pythons dictionaries a dictionary in Python is a built-in
data structure that lets you store and look up data quickly by associating each
piece of data or value with a key it's the best way to save and find data by
way of names or labels and aside from being fast and convenient it also makes
possible some powerful program design techniques let's start with the basics a
dictionary stores values basically any Python object and references those
objects by way of keys the keys have to be a type of object in Python known as a
hashable type strings and integers for instance and since those are the most
common types used as dictionary keys anyway we'll focus on using those here
to give you an idea of the syntax and functionality of a dictionary let's look
at an example when you define a dictionary in code it's offset and curly
braces like so each key in the dictionary is followed by a colon and
then its value and then a comma to separate it from the next key value pair
in the dictionary in this case we're using integers as the keys but again we
can use any hashable type strings would be perfectly valid now each key has to
be unique for a dictionary but each value does not have to be unique in this
example we have lion-king listed twice once for 1994 and once for 2020 when we
want to find a value in a dictionary we look it up by way of its key because of
the way dictionaries work under the hood the time needed to look up any one value
in the dictionary no matter where it might be is about the same the overall
size of the dictionary doesn't make much discernible difference note that we can
also use the value of a variable as the key we're not limited to two hard-coding
the key in the query when we want to add items to a dictionary we just use the
same syntax as the lookup except that we turn this into an assignment instruction
and likewise we can use variables to perform the assignments note that if you
assign a new value to an existing key the old value will be removed from the
dictionary removing an item completely from a dictionary isn't hard either to
do that we just use the del command on the key reference like so now that will
remove both the key and the value if you just want to remove
the value but keep the key as a placeholder you can always just set the
value of the key to none this right here brings up another issue
now you see how trying to retrieve the value for a non-existing key throws an
exception now you may be tempted to throw a try and accept block around it
as a way to handle this and that's a perfectly acceptable way to do it but
dictionaries have an elegant internal mechanism for handling cases where you
may be looking for a key that isn't there the get method on a dictionary
looks for given key but returns none if you can't find it instead of throwing an
exception you can also provide an optional value to return instead of none
if you'd rather have a different default dictionaries can also work as iterables
you can use a for loop to traverse their contents now note that by default when
you use a for loop on a dictionary you only get the keys not the values if you
use the dictionaries items method and iterate through that you get a
collection of key value pairs also worth noting is that up until
Python 3.6 dictionaries didn't have any inherent internal ordering if you
inserted a value into a dictionary there was no guarantee the dictionary would
retain the insertion order of the elements now dictionaries do this the
order in which you insert things is the order in which they'll be returned if
you traverse using for that said dictionaries are still considered
unsorted collections you can't change the insertion order without building a
whole new dictionary so if changing order of elements is important for you
then you're probably better served by using a list that's all for now but in
the next video I'm going to share techniques you can use in your Python
programs that use dictionaries to make it easier to work in situations where
you have to make none of many choices and you don't want to sacrifice
performance if you like this video please leave a comment below don't
forget to subscribe to The IDG Tech Talk channel on YouTube and for more Python
tips be sure to follow us on Facebook YouTube and info WorldCom