Change language

Python | Type conversion in dictionary values

| | |

Method # 1: The Naive Method
In the naive method, we use 2 loops nested. One for all dictionaries in the list, and the second — for dictionary key-value pairs in a specific dictionary.

# Python3 demo code
# Type of conversion to a list of dictation.
# using the naive method

 
# initializing the dictionary list

test_list = [{ ’a’ : ’ 1’ , ’b’ : ’2’ }, { ’ c’ : ’3’ , ’ d’ : ’4’ }]

  
# print original list

print ( "The original list is:" + str (test_list))

 
# using naive method
# Conversation type in the dict list.

for dicts in test_list:

  for keys in dicts:

dicts [keys] = int (dicts [keys])

 
# print result

print ( "The modified converted list is:" +   str (test_list))

Output:

 The original list is: [{’a’:’ 1’, ’b’:’ 2’}, { ’c’:’ 3’, ’d’:’ 4’}] The modified list is: [{’a’: 1,’ b’: 2}, {’c’: 3,’ d’: 4 }] 

Method # 2: Using items () + list comprehension
This can be easily done using just one string using a list comprehension. The items function can be used to retrieve the list values ​​when needed, and the list comprehension part handles the iteration part.

# Python3 demo code
# Type of conversion to dict list.
# use of () elements + list comprehension

 
# initializing the dictionary list

test_list = [{ ’a’ : ’1’ , ’ b’ : ’2’ }, { ’c’ : ’3’ , ’ d’ : ’4’ }]

  
# print original list

print ( "The original list is:" + str (test_list))

 
# use of () elements + list comprehension
# Type of conversation in the dict list.

res = [ dict ([key, int (value)] 

  for key, value in dicts. items ()) 

for dicts in test_list]

  
# print result

print ( "The modified converted list is:" +   str (res))

Output:

 The original list is: [{’b’:’ 2’, ’a’:’ 1’}, {’c’:’ 3’, ’d ’:’ 4’}] The modified converted list is: [{’a’: 1,’ b’: 2}, {’c’: 3,’ d’: 4}] 

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