Learn Data structures with Literal Objects / Dictionaries

Codistwa

Learn Data structures - Objects or Dictionaries

Part 2

Reading time : 2 min

Last update : February 11th, 2022

A literal object in JavaScript or a dictionary in Python is a data structure. It is composed of key/value pairs encapsulated by braces. In general, the key will describe what it receives in value, for example:

{ 
   firstName: Mary 
}
Half off everything 2manning

Definition of an object or dictionary

You can put several types in an object / dictionary.

In the first comic strip, we create a variable represented by a letter in a wagon; it's the same for the second comic strip. Finally, the two variables are combined. It's an object/dictionary.

Code

pythonpython
1fruits = {
2  'color': 'red',
3  'length': 3
4}

Copy by reference

You can change the values of objects; this is called a mutation. We can also create an object from another one to overwrite a value. We say that the value points to the same object (the first one). It is not a copy of the object but its reference. Thus the same information is stored in both objects; they have the same reference and are equal.

There are two variables in two containers (the wagon) that form a set, an object. There is an equal in the first comic strip, which means that the second object is the same as the first. In the second comic strip, we change an element of the second object. For example, we change the letter A into R. So, in the last comic strip, both objects have the same variables because since the second object is a copy, there will be an effect on the value of the first object.

Code

pythonpython
1fruits = {
2    'color': 'red',
3    'length': 3
4}
5
6basket = fruits
7
8# point to the same object
9print(basket['color'])
10
11basket['color'] = 'blue'
12
13print(fruits['color'])  # blue
14
15# changes
16print(basket['color'])
17
18print(basket == fruits)  # True
19

Add a key-value pair

You can add an element directly to the object. JavaScript and Python have a similar method, i.e., you assign a variable directly to a new key, but for Python, there is a more recommended method, .update().

In the beginning, there are two elements, then another is added at the end, represented by a red arrow; we see that it takes position 2.

Code

pythonpython
1basket = {
2    'color': 'red',
3    'length': 3
4}
5
6basket['size'] = 'tall'
7basket.update({'size': 'tail'})
8print(basket) # {'color': 'red', 'length': 3, 'size': 'tail'}

Delete a key-value pair

You can also delete a key/value pair using the del / delete function.

In the beginning, we have an object with two elements. We can easily delete one to obtain an object that has only one value.

Code

pythonpython
1basket = {
2    'color': 'red',
3    'length': 3
4}
5
6del basket['color']
7print(basket) # {'length': 3}
Print booksmanning

Dictionaries / literal objects are useful because they allow you to assemble a set of key / value pairs. You can easily copy and change the values of an object.

Ressources

Source code