Skip to content

Update review-of-dictionaries.md #3046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions python/python-core/more-on-dictionaries/review-of-dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ weather = {'London': 23, 'Barcelona': 28,
'Bucharest': 35}
```

A dictionary is represented by a series of tuples ( key: value ) wrapped in curly braces ( {} ). Another property worth mentioning at this stage is that dictionaries are *unordered data types*, meaning that the order in which tuples are stored and displayed is arbitrary.
A dictionary is represented by a series of tuples ( key: value ) wrapped in curly braces ( {} ).

Being an unordered data type makes it impossible for elements to be accessed via some index. However, we can retrieve any stored value by referencing the related key:
We can retrieve any stored value by referencing the related key:

```python
print(weather['Barcelona'])
Expand Down Expand Up @@ -64,6 +64,18 @@ print(weather.items())'
# ('Bucharest', 35)])
```

Or we can use the `keys()` and `values()` method to get the key or value on a specific index of the dictionary:
```python
secondKey = list(weather.keys())[1]
print(secondKey)
# Barcelona

thirdValue = list(weather.values())[2]
print(thirdValue)
# 35
```


We can update a dictionary by updating an existing entry, adding a new entry or deleting an existing one:

```python
Expand Down