
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Dictionary to List by Repeating Keys Corresponding Value Times
In Python, dictionaries are key-value pairs where each key is associated with a corresponding value. When we want to convert a dictionary to a list by repeating keys, we need to iterate over each key-value pair and repeat the key based on its corresponding value.
Input Output Scenarios
See the following input-output scenarios to understand the concept of converting a dictionary to a list by repeating keys corresponding to the number present in the values.
Input dictionary: {'a': 3, 'b': 2, 'c': 1} Output list: ['a', 'a', 'a', 'b', 'b', 'c']
In the input dictionary the key 'a' has a value of 3, 'b' has a value of 2, and 'c' has a value of 1, so in the output list a is repeated three times, b is repeated two times, and c is repeated one time.
In this article, we will see different approaches to converting a dictionary to a list by repeating the keys based on the numbers present in the corresponding values.
Using the itertools module
Itertools is a module in the Python standard library that provides a collection of functions for efficient and convenient iteration-related operations.
In this approach, we make use of the itertools module's chain.from_iterable function. We iterate over each key-value pair in the dictionary using a generator expression (key, value) for key, value in the dictionary.items(). Inside the generator expression, we will use the itertools.repeat() method to repeat each key, value number of times. The chain.from_iterable function then flattens the resulting iterable into a single sequence.
Example
Here is an example of converting a dictionary to a list by repeating keys corresponding to their values using the itertools module.
import itertools # Define the function def dictionary_to_list(dictionary): result = list(itertools.chain.from_iterable(itertools.repeat(key, value) for key, value in dictionary.items())) return result # Create the dictionary input_dict = {'red': 3, 'yellow': 2, 'green': 3} print('Input dictionary:', input_dict) resultant_list = dictionary_to_list(input_dict) print('Output list:', resultant_list)
Output
Input dictionary: {'red': 3, 'yellow': 2, 'green': 3} Output list: ['red', 'red', 'red', 'yellow', 'yellow', 'green', 'green', 'green']
Using a for loop
In this approach, we will utilize a for loop to iterate over each key-value pair in the dictionary using the items() method. For every pair, we will extend the result list by repeating the key a number of times equal to the corresponding value. This is achieved by utilizing the extend method and the multiplication operator.
Example
In this example, we will use for loop, dict.items(), and list.extend() methods to convert a dictionary to a list by repeating keys corresponding to their values.
# Define the function def dictionary_to_list(dictionary): result = [] for key, value in dictionary.items(): result.extend([key] * value) return result # Create the dictionary input_dict = {'apple': 1, 'banana': 3, 'orange': 4} print('Input dictionary:', input_dict) resultant_list = dictionary_to_list(input_dict) print('Output list:', resultant_list)
Output
Input dictionary: {'apple': 1, 'banana': 3, 'orange': 4} Output list: ['apple', 'banana', 'banana', 'banana', 'orange', 'orange', 'orange', 'orange']
Using the list comprehension
This approach works similarly to the previous one, here we will be using the list comprehension to iterate over the key-value pairs in the dictionary. And for each pair, the key is repeated value number of times using the range() function.
Example
Here's another example to convert a dictionary to a list by repeating keys corresponding to their values using list comprehension.
# Define the function def dictionary_to_list(dictionary): result = [key for key, value in dictionary.items() for _ in range(value)] return result # Create the dictionary input_dict = {'cat': 3, 'dog': 1, 'Parrots': 4} print('Input dictionary:', input_dict) resultant_list = dictionary_to_list(input_dict) print('Output list:', resultant_list)
Output
Input dictionary: {'cat': 3, 'dog': 1, 'Parrots': 4} Output list: ['cat', 'cat', 'cat', 'dog', 'Parrots', 'Parrots', 'Parrots', 'Parrots']
These are the three different examples to convert a dictionary to a list by repeating keys corresponding to their values using python programming.