The Right Way to Access a Dictionary

The Right Way to Access a Dictionary

Be Careful! You might have been doing it wrong.The dictionary is one of the data structures that are ready to use when programming in Python. Before We Start, What is a Dictionary? Dictionary is an unordered and unordered Python collection that maps unique keys to some values. In Python, dictionaries are written by using curly brackets {}

The dictionary is one of the data structures that are ready to use when programming in Python.

Before We Start, What is a Dictionary?

Dictionary is an unordered and unordered Python collection that maps unique keys to some values. In Python, dictionaries are written by using curly brackets {} . The key is separated from the key by a colon : and every key-value pair is separated by a comma ,. Here’s how dictionaries are declared in Python.

#A dictionary containing basketball players with their heights in m
playersHeight = {"Lebron James": 2.06,
                 "Kevin Durant": 2.08, 
                 "Luka Doncic": 2.01,
                 "James Harden": 1.96}

We have created the dictionary, however, what’s good about it if we cannot retrieve the data again right? This is where a lot of people do it wrong. I should admit, I was among them not long ago. After I realize the advantage, I never turn back even once. That’s why I am motivated to share it with you guys.


The Wrong Way

The well-known, or I should say the traditional way to access a value in a dictionary is by referring to its key name, inside a square bracket.

print(playersHeight["Lebron James"]) #print 2.06
print(playersHeight["Kevin Durant"]) #print 2.08
print(playersHeight["Luka Doncic"]) #print 2.01
print(playersHeight["James Harden"]) #print 1.96

Everything seems OK, right? Not so fast! What do you think will happen if you type a basketball player’s name that is not in the dictionary? Look closely

technology data-science python software-engineering programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python for Data Science | Data Science With Python | Python Data Science Tutorial

🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...

Applied Data Science with Python Certification Training Course -IgmGuru

Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now

Best Free Courses For Computer Science, Software Engineering, and Data Science

Best Free Courses For Computer Science, Software Engineering, and Data Science. Become an Expert for Free! Learning Programming, Software Engineering, and Data Science Has Never Been Cheaper

Is Software Engineering a Prerequisite for Data Science?

Find out here. Although data science job descriptions require a range of various skillsets, there are concrete prerequisites that can help you to become a successful data scientist. Some of those skills include, but are not limited to: communication, statistics, organization, and lastly, programming. Programming can be quite vague, for example, some companies in an interview could ask for a data scientist to code in Python a common pandas’ functions, while other companies can require a complete take on software engineering with classes.

Data Science Course in Dallas

Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...