Dictionary Syntax and Key:Value Pairs1:25 with Ashley Boucher
Learn how to write a Python dictionary and recognize and create key:value pairs.
In this video, we're going to discuss the syntax for writing dictionaries and 0:00 learning more about key value pairs. 0:04 Just like lists are wrapped in square brackets, 0:07 dictionaries are wrapped in curly braces. 0:09 Each element inside the curly braces is a key value pair, 0:12 separated from the previous by a comma. 0:15 A key is a label or an identifier for its associated value. 0:18 For instance, if I'm using a dictionary to model or store data about myself, 0:22 and one piece of data I want to include is my name, I create a key-value pair 0:27 where the key is the string name, and the value is the string Ashley. 0:32 I could also use a key-value pair to store my profession. 0:37 Storing key value pairs in a dictionary allows you to group related data together 0:41 and keep it organized. 0:45 Unlike a list or a tuple, the order of the key value pairs doesn't matter. 0:47 It does however matter that the keys be unique. 0:51 If you add a key value pair to your dictionary that uses a duplicate key, 0:54 the original value associated with that key will be forgotten. 0:58 Keys can be any immutable type, strings, integers, even tuples, but 1:02 only if every item in the tuple is also immutable. 1:07 Values can be any type. 1:10 Dictionaries can also grow or 1:12 shrink as needed, key value pairs can always be added or deleted. 1:14 Now that you know a little bit about how to write a dictionary, join me in the next 1:18 video to learn about accessing data that's inside the dictionary. 1:22
You need to sign up for Treehouse in order to download course files.Sign up