Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Split dictionary keys and values into separate lists

Python | Split dictionary keys and values into separate lists

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.

RELATED ARTICLES

Most Popular

Recent Comments