Change language

Python | Split dictionary keys and values ​​into separate lists

| | |

Method # 1: Using built-in functions

# Python code for demonstration
# split the dictionary
# into keys and values ​​

 
# _dictionary initialization

ini_dict = { ’a’ : ’akshat’ , ’ b’ : ’bhuvan’ , ’ c ’ : ’ chandan’ }

 
# print iniial_dictionary

print ( "intial_dictionary" , str (ini_dict))

 
# split the dictionary into keys and values ​​

keys = ini_dict.keys ()

values ​​ = ini_dict.values ​​()

 
# print keys and values ​​separately

print ( " keys: " , str (keys))

print ( "values:" , str (values))

Output:

 intial_dictionary {’a’:’ akshat’, ’b’:’ bhuvan’, ’c’:’ chandan’} keys: dict_keys ([’a’,’ b’, ’ c’]) values: dict_values ​​([’akshat’,’ bhuvan’, ’chandan’]) 

Method # 2: Using zip ()

# Python code for demonstration
# split dictionary
# to keys and values ​​

 
# _dictionary initialization

ini_dict = { ’a’ : ’akshat’ , ’ b’ : ’bhuvan’ , ’ c’ : ’ chandan’ }

 
# print iniial_dictionary

print ( " intial_dictionary " , str (ini_dict))

 
# split the dictionary for keys and values ​​

keys, values ​​ = zip ( * ini_dict.items ())

  
# print keys and values ​​separately

print ( "keys:" , str (keys))

print ( "values:" , str (values))

Output:

 intial_dictionary {’a’:’ akshat’, ’c’:’ chandan’, ’b’:’ bhuvan’} keys: (’a’,’ c’, ’b’) values: (’ akshat ’,’ chandan’, ’bhuvan’) 

Method # 3: Using items()

# Python code for demonstration
# split dictionary
# to keys and values ​​

 
# _dictionary initialization

ini_dict = { ’a’ : ’ akshat’ , ’b’ : ’ bhuvan’ , ’c’ : ’ chandan ’ }

  
# print iniial_dictionary

print ( "intial_dictionary " , str (ini_dict))

 
# split the dictionary into keys and values ​​

keys = []

values ​​ = []

items = ini_dict.items ()

for item in items:

  keys.append (item [ 0 ]), values.append (item [ 1 ])

 
# print keys and values ​​separately

print ( "keys:" , str (keys))

print ( "values:" , str (values))

Exit :

 intial_dictionary {’b’:’ bhuvan’, ’c’:’ chandan’, ’a’:’ akshat’} keys: [’b’,’ c’, ’a’] values: [’ bhuvan’, ’chandan’,’ akshat’] 

Python | Split dictionary keys and values ​​into separate lists __dict__: Questions

Python | Split dictionary keys and values ​​into separate lists Python functions: Questions

Shop

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

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method