How Python Dictionary functions work

In our previous section, we have seen a few examples of Python dictionary functions. In this section we will discuss more.

Let’s start with the popitem() method.

This method removes and returns the last key, value pair inserted into the Python Dictionary.

However, the popitem() doesn’t take any arguments.

Let’s take a look at one example.

robot = {'name': 'Robu', 'age': 12352, 'isIntelligent': True}

print('Before removing last item, Robot = ', robot)

# Since the pair "'isIntelligent': True" is inserted at the last, so it will be removed.
result = robot.popitem()

print('Return Value = ', result)
print('After removing last item, Robot = ', robot)

# We are inserting a new key-value pair
robot['profession'] = 'Mathematician'

print('After inserting last item, Robot = ', robot)

result = robot.popitem()

print('Return Value = ', result)
print('After removing last item again, Robot = ', robot)

And here goes the output.

Before removing last item, Robot =  {'name': 'Robu', 'age': 12352, 'isIntelligent': True}
Return Value =  ('isIntelligent', True)
After removing last item, Robot =  {'name': 'Robu', 'age': 12352}
After inserting last item, Robot =  {'name': 'Robu', 'age': 12352, 'profession': 'Mathematician'}
Return Value =  ('profession', 'Mathematician')
After removing last item again, Robot =  {'name': 'Robu', 'age': 12352}

From the output, we see that the popitem() method returns the last key, value pair as its result. 

But at the same time it removes the pair also.

The version Python 3.7 has introduced it.


If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.

But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


Before that the popitem() method returns and removes an arbitrary element.

Not the last item.

On the other hand, the setdefault() method returns the value of a key.

As a result, the key should be in the dictionary.

If it is not there, in that case, it inserts it.

Let’s see an example.

# key is in the dictionary

robot = {'name': 'Robu', 'age': 4785}

age = robot.setdefault('age')
print('robot = ', robot)
print('Age = ', age)

#output
robot =  {'name': 'Robu', 'age': 4785}
Age =  4785

We have exhibited one example where the key is in the dictionary.

However, we can see another example, where the key is not there. Right?

# key is not in the dictionary

robot = {'name': 'Robu'}

# key is not in the dictionary
salary = robot.setdefault('salary')
print('robot = ', robot)
print('salary = ', salary)

# key is not in the dictionary
# default_value is provided
age = robot.setdefault('age', 2563)
print('robot = ', robot)
print('age = ', age)

# output:
robot =  {'name': 'Robu', 'salary': None}
salary =  None
robot =  {'name': 'Robu', 'salary': None, 'age': 2563}
age =  2563

In this section we will finally see the values() method. 

This method returns all the values present in the dictionary as the name suggests.

Let’s see the example.

names = { 1: 'John', 2: 'Json', 3: 'James' }

print(names.values())

# output
dict_values(['John', 'Json', 'James'])

If we update the value, the change is automatically displayed in the dictionary.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart, Python and TensorFlow

C, C++ Java, and Game Development

Twitter

Comments

Leave a Reply