
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Convert List to Index and Value dictionary
When it is required to convert a list into an index value dictionary, the ‘enumerate' and a simple iteration are used.
Example
Below is a demonstration of the same −
my_list = [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The sorted list is ") print(my_list) index, value = "index", "values" my_result = {index : [], value : []} for id, vl in enumerate(my_list): my_result[index].append(id) my_result[value].append(vl) print("The result is :") print(my_result)
Output
The list is : [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89] The sorted list is [223, 99, 94, 89, 67, 51, 32, 28, 12, 11, 0] The result is : {'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'values': [223, 99, 94, 89, 67, 51, 32, 28, 12, 11, 0]}
Explanation
A list of integers is defined and is displayed on the console.
The list is sorted in reverse order and is displayed on the console.
The index and values are initialized for display purposes.
The list is iterated over using enumerate, and the index and values are appended to the empty list.
This is the output that is displayed on the console.
- Related Articles
- Python program to Convert Matrix to Dictionary Value List
- Python – Replace value by Kth index value in Dictionary List
- Python – Sort Dictionary List by Key’s ith Index value
- How to convert Python Dictionary to a list?
- How to convert list to dictionary in Python?
- Dictionary with index as value in Python
- Convert dictionary to list of tuples in Python
- Python - Clearing list as dictionary value
- Convert key-values list to flat dictionary in Python
- Accessing index and value in a Python list
- Python – Index Value repetition in List
- Python program to convert a list of tuples into Dictionary
- Convert string dictionary to dictionary in Python
- Python - Convert list of nested dictionary into Pandas Dataframe
- How to convert Javascript dictionary to Python dictionary?

Advertisements