Python | Split dictionary keys and values into separate lists
Given a dictionary, the task is to split that dictionary into keys and values into different lists. Let’s discuss the different ways we can do this.
Method #1: Using built-in functions
# Python code to demonstrate # to split dictionary # into keys and values # initialising _dictionary ini_dict = {'a' : 'akshat', 'b' : 'bhuvan', 'c': 'chandan'} # printing iniial_dictionary print("intial_dictionary", str(ini_dict)) # split dictionary into keys and values keys = ini_dict.keys() values = ini_dict.values() # printing keys and values separately print ("keys : ", str(keys)) print ("values : ", str(values)) |
chevron_right
filter_none
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 to demonstrate # to split dictionary # into keys and values # initialising _dictionary ini_dict = {'a' : 'akshat', 'b' : 'bhuvan', 'c': 'chandan'} # printing iniial_dictionary print("intial_dictionary", str(ini_dict)) # split dictionary into keys and values keys, values = zip(*ini_dict.items()) # printing keys and values separately print ("keys : ", str(keys)) print ("values : ", str(values)) |
chevron_right
filter_none
Output:
intial_dictionary {'a': 'akshat', 'c': 'chandan', 'b': 'bhuvan'}
keys : ('a', 'c', 'b')
values : ('akshat', 'chandan', 'bhuvan')
Method #3: Using items()
# Python code to demonstrate # to split dictionary # into keys and values # initialising _dictionary ini_dict = {'a' : 'akshat', 'b' : 'bhuvan', 'c': 'chandan'} # printing iniial_dictionary print("intial_dictionary", str(ini_dict)) # split dictionary into keys and values keys = [] values = [] items = ini_dict.items() for item in items: keys.append(item[0]), values.append(item[1]) # printing keys and values separately print ("keys : ", str(keys)) print ("values : ", str(values)) |
chevron_right
filter_none
Output:
intial_dictionary {'b': 'bhuvan', 'c': 'chandan', 'a': 'akshat'}
keys : ['b', 'c', 'a']
values : ['bhuvan', 'chandan', 'akshat']
Recommended Posts:
- Python | Test if dictionary contains unique keys and values
- Python | Find keys with duplicate values in dictionary
- Python program to Swap Keys and Values in Dictionary
- Python | Combine two dictionary adding values for common keys
- Python | Split dictionary of lists to list of dictionaries
- Python | Split and Pass list as separate parameter
- Python | Convert column to separate elements in list of lists
- Python | Add new keys to a dictionary
- Python | Grouping dictionary keys by value
- Python | Add keys to nested dictionary
- Python | Count keys with particular value in dictionary
- Python | Minimum value keys in Dictionary
- Python | Get total keys in dictionary
- Python | Get the number of keys with given value N in dictionary
- Python Dictionary | keys() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh



