- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to Convert Matrix to Dictionary Value List
When it is required to convert a matrix to a dictionary value list, a simple dictionary comprehension can be used.
Example
Below is a demonstration of the same
my_list = [[71, 26, 35], [65, 56, 37], [89, 96, 99]] print("The list is :") print(my_list) my_result = {my_index + 1 : my_list[my_index] for my_index in range(len(my_list))} print("The result is:") print(my_result)
Output
The list is : [[71, 26, 35], [65, 56, 37], [89, 96, 99]] The result is: {1: [71, 26, 35], 2: [65, 56, 37], 3: [89, 96, 99]}
Explanation
A list of list is defined and is displayed on the console.
A dictionary comprehension is used to iterate over the list, and check specific elements of the list using list slicing and indexing.
This is converted to a dictionary and assigned to a variable.
This is displayed as output on the console.
Advertisements