- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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.
- Related Articles
- Python – Convert List to Index and Value dictionary
- Python program to convert a list of tuples into Dictionary
- How to convert Python Dictionary to a list?
- How to convert list to dictionary in Python?
- Convert dictionary to list of tuples in Python
- Python Program to Convert Matrix to String
- Convert key-values list to flat dictionary in Python
- How to convert a dictionary to a matrix or nArray in Python?
- How to convert Javascript dictionary to Python dictionary?
- Convert string dictionary to dictionary in Python
- Python - Clearing list as dictionary value
- Python program to convert a list into a list of lists using a step value
- Python program to convert a list to string
- Python program to get maximum of each key Dictionary List
- Python – Convert Integer Matrix to String Matrix

Advertisements