![]() These two elements form what is called a key-value pair (a key with its corresponding value). Values: these are the values that you can access with their corresponding key.In dictionaries, to access a value, you use the key, which is a value itself. Keys are the equivalent of "indices" in strings, lists, and tuples. Keys: a key is a value used to access another value.Since a dictionary "connects" two values, it has two types of elements: Let's see the different elements that make a dictionary. In this example,"Gino" is "connected" to the integer 15 and the string "Nora" is "connected" to the integer 30. Lets see the different elements that make a dictionary. In this example,'Gino' is 'connected' to the integer 15 and the string 'Nora' is 'connected' to the integer 30. With a dictionary, you can 'connect' a value to another value to represent the relationship between them in your code. With a dictionary, you can "connect" a value to another value to represent the relationship between them in your code. A Python dictionary looks like this (see below). Meet DictionariesĪ Python dictionary looks like this (see below). This is where Python dictionaries come to the rescue. If you use nested lists, things would get very complex and inefficient after adding only a few items because you would need to use two or more indices to access each value, depending on the final list. We want to keep the "connection" between them. Let's say that we want to store names of students and "connect" each name with the grades of each particular student. ![]() You can see an example right here:īut what if we need to store two related values and keep this "connection" in our code? Right now, we only have single, independent values stored in a list. ![]() Indices start from zero for the first element and increase by one for every subsequent element in the list. When you work with lists in Python, you can access an element using a index, an integer that describes the position of the element in the list. To illustrate this, let me do a quick comparison with another data type that you are probably familiar with: lists. Let's start by discussing the importance of dictionaries. Let's begin! ⭐️ □ Dictionaries in Context
0 Comments
Leave a Reply. |