Thursday, April 25, 2024
HomePythonExtract a Subset from a Dictionary in Python – Finxter

Extract a Subset from a Dictionary in Python – Finxter


Drawback Formulation and Resolution Overview

This text will present you the right way to extract a subset from a Dictionary in Python.

Our article works with a Dictionary known as vehicles, containing 30 key:worth pairs: a reputation and related horsepower.

{'Chevrolet Chevelle Malibu': '130.0', 'Buick Skylark 320': '165.0', 'Plymouth Satellite tv for pc': '150.0', 'AMC Insurgent SST': '150.0', 'Ford Torino': '140.0', 'Ford Galaxie 500': '198.0', 'Chevrolet Impala': '220.0', 'Plymouth Fury iii': '215.0', 'Pontiac Catalina': '225.0', 'AMC Ambassador DPL': '190.0', 'Citroen DS-21 Pallas': '115.0', 'Chevrolet Chevelle Concours (sw)': '165.0', 'Ford Torino (sw)': '153.0', 'Plymouth Satellite tv for pc (sw)': '175.0', 'AMC Insurgent SST (sw)': '175.0', 'Dodge Challenger SE': '170.0', "Plymouth 'Cuda 340": '160.0', 'Ford Mustang Boss 302': '140.0', 'Chevrolet Monte Carlo': '150.0', 'Buick Property Wagon (sw)': '225.0', 'Toyota Corolla Mark ii': '95.0', 'Plymouth Duster': '95.0', 'AMC Hornet': '97.0', 'Ford Maverick': '85.0', 'Datsun PL510': '88.0', 'Volkswagen 1131 Deluxe Sedan': '46.0', 'Peugeot 504': '87.0', 'Audi 100 LS': '90.0', 'Saab 99e': '95.0', 'BMW 2002': '113.0'}

💬 Query: How would we write code to extract a subset from a Dictionary?

We will accomplish this activity by one of many following choices:

Additionally, be happy to take a look at our full information on an identical subject, i.e., lowering the variety of components in a dictionary:

👉 Really helpful Tutorial: Filter a Dictionary in Python? (… The Most Pythonic Means)


Technique 1: Use dict.objects() and record

This instance makes use of dict.objects() to extract the primary 5 (5) key:worth pairs from a Dictionary.

first_five = dict(record(vehicles.objects())[:4])
print(first_five)

The above is a one-liner that extracts the primary 5 (5) key:worth pairs from the vehicles dictionary by doing the next:

  • The record() perform is named and handed one (1) argument, vehicles.objects().
  • Then, the primary 5 (5) entries are extracted utilizing slicing ([:4]).
  • Subsequent, the record is transformed again right into a Dictionary.

The outcomes are output to the terminal.

{'Chevrolet Chevelle Malibu': '130.0', 'Buick Skylark 320': '165.0', 'Plymouth Satellite tv for pc': '150.0', 'AMC Insurgent SST': '150.0'}

Technique 2: Use Dictionary Comprehension and sorted()

This instance makes use of Dictionary Comprehension and sorted() to return the primary three (3) sorted key:worth Dictionary pairs.

sorted_three = {ok: vehicles[k] for ok in sorted(vehicles.keys())[:3]}
print(sorted_three)

The above code types the vehicles Dictionary based mostly on the key portion of the key:worth pair and returns the primary three (3) pairs that consequence from the kind.

The outcomes are output to the terminal.

{'AMC Ambassador DPL': '190.0', 'AMC Hornet': '97.0', 'AMC Insurgent SST': '150.0'}

Technique 3: Use Dictionary Comprehension dict.objects()

This instance makes use of Dictionary Comprehension and dict.objects() to extract values from a key:worth pair the place the worth is bigger than a specified quantity.

hpower = {key: worth for key, worth in vehicles.objects() if float(worth) >= 220.0}
print(hpower)

The above code makes use of Dictionary Comprehension to iterate by the vehicles Dictionary. Throughout the iteration, every worth is transformed from a string to a float and examined to see whether it is better than or equal to (>=) 220.0. If True, the key:worth pair is appended to hpower.

The outcomes are output to the terminal.

{'Chevrolet Impala': '220.0', 'Pontiac Catalina': '225.0', 'Buick Property Wagon (sw)': '225.0'}

Technique 4: Use Dictionary Comprehension and in

This instance makes use of Dictionary Comprehension and in to extract key:worth pairs the place the worth is within the specified record.

hpower = {key: worth for key, worth in vehicles.objects() if float(worth) in [115.0, 220.0]}
print(hpower)

The above code makes use of Dictionary Comprehension to iterate by the vehicles Dictionary. Throughout the iteration, every worth is transformed from a string to a float and examined to see if the worth exists within the specified record. If that is true, the key:worth pair is appended to hpower.

The outcomes are output to the terminal.

{'Chevrolet Impala': '220.0', 'Citroen DS-21 Pallas': '115.0'}

Technique 5: Use Dictionary Comprehension and enumerate()

This instance makes use of Dictionary Comprehension and enumerate(). This perform permits you to loop over an iterable and related counter to extract particular key:worth pairs based mostly on a situation.

top_ten = {kv[0]:kv[1] for i, kv in enumerate(vehicles.objects()) if i <= 10}
print(top_ten)

This code makes use of Dictionary Comprehension and enumerate() to iterate by the loop. If the counter (i) is lower than or equal to (<=) 10, then key:worth pair is appended to top_ten.

The outcomes are output to the terminal

{'Chevrolet Chevelle Malibu': '130.0', 'Buick Skylark 320': '165.0', 'Plymouth Satellite tv for pc': '150.0', 'AMC Insurgent SST': '150.0', 'Ford Torino': '140.0', 'Ford Galaxie 500': '198.0', 'Chevrolet Impala': '220.0', 'Plymouth Fury iii': '215.0', 'Pontiac Catalina': '225.0', 'AMC Ambassador DPL': '190.0', 'Citroen DS-21 Pallas': '115.0'}

Abstract

This text has offered 5 methods to extract a subset from a dictionary to pick the very best match to your coding necessities.

Good Luck & Completely happy Coding!


Programming Humor

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments