Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to 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.
Method 1: Using itertools Module
Itertools is a module in the Python standard library that provides a collection of functions for efficient and convenient iteration-related operations. We use itertools.repeat() to repeat each key and chain.from_iterable() to flatten the result ?
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)
Input dictionary: {'red': 3, 'yellow': 2, 'green': 3}
Output list: ['red', 'red', 'red', 'yellow', 'yellow', 'green', 'green', 'green']
Method 2: Using For Loop with extend()
In this approach, we iterate over each key-value pair using items() and extend the result list by repeating the key using list multiplication ?
# 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)
Input dictionary: {'apple': 1, 'banana': 3, 'orange': 4}
Output list: ['apple', 'banana', 'banana', 'banana', 'orange', 'orange', 'orange', 'orange']
Method 3: Using List Comprehension
This approach uses list comprehension with nested loops. The outer loop iterates over key-value pairs, and the inner loop repeats each key the specified number of times ?
# 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, 'parrot': 4}
print('Input dictionary:', input_dict)
resultant_list = dictionary_to_list(input_dict)
print('Output list:', resultant_list)
Input dictionary: {'cat': 3, 'dog': 1, 'parrot': 4}
Output list: ['cat', 'cat', 'cat', 'dog', 'parrot', 'parrot', 'parrot', 'parrot']
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| itertools | Medium | Excellent | Large datasets |
| For loop | High | Good | Beginners |
| List comprehension | Medium | Very Good | Concise solutions |
Conclusion
Use list comprehension for concise and readable code. For large datasets, itertools provides the best performance. The for loop approach is most suitable for beginners due to its clear and simple logic.
