Home » Search results for 'python dictionary'

What is a Python Dictionary? Explanation and Code Examples

Python dictionary

Here’s everything you need to know about the Dictionary data type in Python, what it can store, and how to use it. Python has several built-in data types for storing data (more than other languages like JavaScript) – from basic types like integers, floating-point numbers, and strings to more complex types like lists, tuples, and dictionaries. What is a Type? A variable’s type defines what kind of value it can store and what can be done with it. What is a Dictionary? A dictionary is a variable type … Read more

Home » Search results for 'python dictionary'

How to Create a Dictionary in Python from a JSON API/URL

How to Create a Dictionary in Python from a JSON API/URL

This tutorial will show you how to create Python dictionary from JSON data retrieved from an API via HTTP. This is useful for retrieving data to use in your Python scripts and applications. Step 1: Install the request Library The easiest way to interact with web resources in Python is using the requests library. Install it by running: You may want to use a virtual environment to keep the dependencies for all of your Python projects separate. Step 2: Know the Format of the JSON Data you are Retrieving JSON … Read more

Home » Search results for 'python dictionary'

How to Sort a Dictionary by Value in Python [Examples]

Python sort dictionary by value

This article will show you how to sort a dictionary by the values it contains in the Python programming language. Code examples are included. What is a Dictionary? A dictionary is one of Pythons built in variable types for storing data. Dictionaries are similar to associative arrays or hash tables in other languages. A dictionary contains a collection of values or objects. Unlike lists and arrays, rather than their values being stored at a specific index (the ordered position in the sequence of stored values), values in a dictionary … Read more

Home » Search results for 'python dictionary'

How to Iterate/Loop over a Dictionary in Python [Examples]

Python iterate loop dictionary

This article will show you how to iterate/loop over the keys and values in a Python dictionary, and provide code examples. What is a Python Dictionary? In Python, a dictionary is a data type that contains a collection of objects – but unlike a list or array, rather than their values being recorded at a specific index (or ordered position in the sequence of stored values), they are stored in a key – a string identifier which both tells you what a value is for, and which … Read more

Home » Search results for 'python dictionary'

How to Add/Append Items to a Dictionary in Python [Examples]

Add Item to Python Dictionary

This short tutorial will show you how to add single or multiple items (as key:value pairs) to a dictionary in the Python programming language. What is a Dictionary in Python? We’ve covered what dictionaries are in our article here. There are a couple of ways to add items to a dictionary, so we’ve put together this article separately to make sure we’ve got everything covered! Checking Whether a Key Exists in a Dictionary As outlined in the article linked above, dictionaries store data as a series of key:value pairs. Before … Read more

Home » Search results for 'python dictionary'

How to Slice a Dictionary in Python, With Examples

Python Dictionary slice()

We’ve covered how to use the slice function to slice strings, tuples, lists, and other sequences. This tutorial will show you how to slice dictionaries, with examples. Dictionaries Not sure what a dictionary is in the Python programming language? Get up to speed with our article here. The slice() Function and the islice() Function Check out the full article here on how the slice() function works – we’ve covered it in-depth. Unfortunately, the slice() function built-in to Python does not work with dictionaries. If you do try to use slice() with a dictionary type variable, you will receive the following error: TypeError: … Read more

Home » Search results for 'python dictionary'

Python NumPy Crash Course – A Simple Tutorial and Example

Python Numpy Course Tutorial Example

Python’s NumPy library is the standard for working with numerical data. This article will explain why that is, and how to use it. In addition to this, code examples for basic usage will be shown, and we will build a real-world example that implements NumPy to work with some example data. Python and Arrays Whether you are working with data collected for scientific or business reasons, it is usually collected in the form of arrays of data presented as tables with single or multiple columns. … Read more

Home » Search results for 'python dictionary'

How to Merge Dictionaries in Python, With Examples

Python Merge Dictionaries

This article will show you the simplest ways to merge two or more dictionaries in the Python programming language. Note that the code examples in this article are written for Python version 3 – and will not work with Python version 2! What is a Dictionary in Python? We’ve covered what dictionaries are in our article here. As there are a couple of ways to merge multiple dictionaries in Python, we’ve put together this article separately to make sure we’ve got everything covered! Merging/Joining Using Dictionary Union … Read more

Home » Search results for 'python dictionary'

How to Use ‘for’ Loops in Python, With Examples

Python for Loops

This article will show you how to use for loops in Python 3 and provide examples. Loops are one of the fundamentals of programming. So you’ll use them almost constantly. Data in computer software is often stored in arrays (or lists), for example, a record of students enrolled in a course, with each item in a list representing a student. These arrays of data need to be processed – and this is done using loops that step through each item in the array and perform some … Read more

Home » Search results for 'python dictionary'

How to Round Numbers Up/Down/Nearest in Python

Python Round Numbers

This article will show you how to round numbers up, down, and to the nearest 1, 10 100 (or any number) in the Python programming language. Truncation vs. Rounding Truncation isn’t technically rounding, but it serves a similar purpose, so I’ll include it here. It’s the simplest way to shorten a number – just cutting off the characters at the end until it’s in the format you require. Rounding gets the nearest number, whereas truncation cuts off digits from the number. For example: To round 2.9 to … Read more