Open In App

Python | Split dictionary keys and values into separate lists

Last Updated : 07 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionary, the task is to split a dictionary in python into keys and values into different lists. Let’s discuss the different ways we can do this. 

Example

Input: {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
Output: 
    keys:   ['a', 'b', 'c']
    values: ['akshat', 'bhuvan', 'chandan']

Method 1: Split dictionary keys and values using inbuilt functions 

Here, we will use the inbuilt function of Python that is .keys() function in Python, and .values() function in Python to get the keys and values into separate lists.

Python3




# initialising _dictionary
ini_dict = {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
 
# printing initial_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))


Output:

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

Time Complexity: O(n)
Auxiliary Space: O(n)

 Method 2: Split dictionary keys and values using zip() 

Here, we will use the zip() function of Python to unpack the keys and values from the dictionary.

Python3




# Python code to demonstrate
# to split dictionary
# into keys and values
 
# initialising _dictionary
ini_dict = {'a' : 'akshat', 'b' : 'bhuvan', 'c': 'chandan'}
 
# printing initial_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))


Output:

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

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

 Method 3: Split dictionary keys and values using items() 

Here, we will use a Python loop and append the keys and values to the list using .items() function that will extract the keys and values from the dictionary.

Python3




# initialising _dictionary
ini_dict = {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
 
# printing initial_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))


Output:

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

Method 4 : Iterating a for loop over dictionary

Python3




# initialising _dictionary
ini_dict = {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
 
# printing initial_dictionary
print("intial_dictionary", str(ini_dict))
 
# split dictionary into keys and values
keys = []
values = []
for i in ini_dict:
    keys.append(i)
    values.append(ini_dict[i])
# printing keys and values separately
print("keys : ", str(keys))
print("values : ", str(values))


Output

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

Method 5: Using List Comprehension

Step-by-step approach:

  • Create two separate lists using list comprehension to store keys and values of the dictionary.
  • Iterate over the dictionary using the items() method to extract the keys and values.
  • Store the keys and values in separate lists using list comprehension.
  • Print the keys and values lists.

Below is the implementation of the above approach:

Python3




# initialising _dictionary
ini_dict = {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
 
# printing initial_dictionary
print("intial_dictionary", str(ini_dict))
 
# split dictionary into keys and values using list comprehension
keys = [key for key in ini_dict]
values = [ini_dict[key] for key in ini_dict]
 
# printing keys and values separately
print("keys : ", str(keys))
print("values : ", str(values))


Output

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

Time complexity: O(n) where n is the number of key-value pairs in the dictionary. List comprehension takes linear time complexity to create a list.
Auxiliary space: O(n) as we are creating two lists to store the keys and values of the dictionary.

Method 6: use the built-in function map() in combination with the dict.keys() and dict.values() methods

  • Use the map() function to apply the str() function to each element of the dict.keys() and dict.values() iterators, respectively. Store the resulting iterators in the keys and values variables as lists using the list() function.
  • Print the keys and values separately to verify their contents.

Python3




# Python program for the above approach
 
 
# initialising dictionary
ini_dict = {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
 
# printing initial_dictionary
print("initial_dictionary", str(ini_dict))
 
# split dictionary into keys and values
# using map() and dict methods
keys = list(map(str, ini_dict.keys()))
values = list(map(str, ini_dict.values()))
 
# Print keys and values separately
print("keys : ", str(keys))
 
print("values : ", str(values))


Output

initial_dictionary {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
keys :  ['a', 'b', 'c']
values :  ['akshat', 'bhuvan', 'chandan']

Time Complexity: O(n), where n is the number of items in the dictionary. The map() function and list conversion both have linear time complexity, and the dict.keys() and dict.values() methods are also linear in the size of the dictionary.

Auxiliary Space: O(n), where n is the number of items in the dictionary. This is because the method creates two new lists of the same size as the dictionary.



Similar Reads

Python - Unique value keys in a dictionary with lists as values
Sometimes, while working with Python dictionaries, we can have problem in which we need to extract keys with values that are unique (there should be at least one item not present in other lists), i.e doesn't occur in any other key's value lists. This can have applications in data preprocessing. Lets discuss certain ways in which this task can be pe
4 min read
Python - Append Dictionary Keys and Values ( In order ) in dictionary
Given a dictionary, perform append of keys followed by values in list. Input : test_dict = {"Gfg" : 1, "is" : 2, "Best" : 3} Output : ['Gfg', 'is', 'Best', 1, 2, 3] Explanation : All the keys before all the values in list. Input : test_dict = {"Gfg" : 1, "Best" : 3} Output : ['Gfg', 'Best', 1, 3] Explanation : All the keys before all the values in
5 min read
Python - Split Dictionary values on size limit of values
Given a dictionary with string values, the task is to write a python program to split values if the size of string exceeds K. Input : {1 : "Geeksforgeeks", 2 : "best for", 3 : "all geeks"}, limit = 5Output : {1: 'Geeks', 2: 'forge', 3: 'eks', 4: 'best ', 5: 'for', 6: 'all g', 7: 'eeks'}Explanation : All string values are capped till length 5. New v
8 min read
Python - Test for Even values dictionary values lists
Given a dictionary with lists as values, map Boolean values depending upon all values in List are Even or not. Input : {"Gfg" : [6, 8, 10], "is" : [8, 10, 12, 16], "Best" : [10, 16, 14, 6]} Output : {'Gfg': True, 'is': True, 'Best': True} Explanation : All lists have even numbers. Input : {"Gfg" : [6, 5, 10], "is" : [8, 10, 11, 16], "Best" : [10, 1
8 min read
Different ways of sorting Dictionary by Keys and Reverse sorting by keys
Prerequisite: Dictionaries in Python A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. We can access the values of the dictionary using keys. In this article, we will discuss 10 different ways of sorting the Python dictionary by keys and a
8 min read
Python | Split and Pass list as separate parameter
With the advent of programming paradigms, there has been need to modify the way one codes. One such paradigm is OOPS. In this, we have a technique called modularity, which stands for making different modules/functions which perform independent tasks in program. In this, we need to pass more than just variable, but a list as well. Let's discuss cert
4 min read
Python - Extract selective keys' values Including Nested Keys
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract selective keys' values. This problem has been solved earlier, but sometimes, we can have multiple nestings and certain keys may be present in inner records. This problem caters all the nestings for extraction of keys' values. Let's discuss certain w
7 min read
Python - Keys with shortest length lists in dictionary
Sometimes, while working with Python lists, we can have problem in which we need to return the keys which have minimum lengths of lists as values. This can have application in domains in which we work with data. Lets discuss certain ways in which this task can be performed. Method #1: Using len() + loop + items() The combination of above functions
4 min read
Python | Convert column to separate elements in list of lists
There are instances in which we might require to extract a particular column of a Matrix and assign its each value as separate entity in list and this generally has a utility in Machine Learning domain. Let's discuss certain ways in which this action can be performed.Method #1 : Using list slicing and list comprehension The functionality of list sl
4 min read
Python Program to create a sub-dictionary containing all keys from dictionary list
Given the dictionary list, our task is to create a new dictionary list that contains all the keys, if not, then assign None to the key and persist of each dictionary. Example: Input : test_list = [{'gfg' : 3, 'is' : 7}, {'gfg' : 3, 'is' : 1, 'best' : 5}, {'gfg' : 8}]Output : [{'is': 7, 'best': None, 'gfg': 3}, {'is': 1, 'best': 5, 'gfg': 3}, {'is':
8 min read