Change language

Python | Converting types of dictionary elements

| | |

Method # 1: Using a loop
This problem can be solved naively with loops. In this we will loop for each key and value, then typecast the keys and values ​​separately and return the desired integer container.

# Python3 code to demonstrate how it works
# Type of dictionary element conversion
# Using a loop

 
# Initialize dictionary

test_dict = { ’1’ : [ ’4’ , ’ 5’ ], ’4’ : [ ’6’ , ’ 7’ < code class = "plain">], ’10’ : [ ’8’ ]}

  
# print the original dictionary

print ( " The original dictionary: " +   str (test_dict))

 
# Using a loop
# Conversion type for dictionary elements

res = {}

for key, value in test_dict.items ():

res [ int (key)] = [ int (item) for item in value]

 
# print result

print ( "Dictionary after type conversion:" + str (res))

Output:

The original dictionary: {’10’: [’8’], ’4’: [’6’, ’7’] , ’1’: [’4’, ’5’]}
Dictionary after type conversion: {1: [4, 5], 10: [8], 4: [6, 7]}

Method # 2: Using vocabulary comprehension
This task is Can be easily accomplished using a one-line abbreviation using vocabulary comprehension. This offers a shorter alternative to the looping method discussed above and therefore recommended.

# Python3 code to demonstrate how it works
# Type of dictionary element conversion
# Using dictionary understanding

  
# Initialize dictionary

test_dict = { ’ 1’ : [ ’4’ , ’ 5’ ], ’4’ : [ ’ 6’ , ’7’ ], ’10’ : [ ’ 8’ ]}

 
# print the original dictionary

print ( "The original dictionary:" +   str (test_dict))

 
# Using dictionary comprehension
# Conversion type for dictionary elements

res = { int (key): [ int (i) for i in val]

  for key, val in test_dict.items ()}

 
# print result

print ( "Dictionary after type conversion:" + str (res))

Output:

The original dictionary: {’10’: [’8’], ’4’: [ ’6’, ’7’], ’1’: [’4’, ’5’]}
Dictionary after type conversion: {1: [4, 5], 10: [8], 4: [ 6, 7]}

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically