Converting a Python list to a dictionary with the index as value is useful when you need to map each element to its position inside the list.
In this article, we will learn multiple methods to convert a list into a dictionary where keys are list items and values are their indexes.
Example -
fruits = ['apple', 'banana', 'cherry']
output = {'apple': 0, 'banana': 1, 'cherry': 2}
Method 1: Using Dictionary Comprehension (Best & Pythonic Way)
fruits = ['apple', 'banana', 'cherry']
fruit_dict = {value: index for index, value in enumerate(fruits)}
print(fruit_dict)
Output:
{'apple': 0, 'banana': 1, 'cherry': 2}
Why use this?
> Works for any iterable
> Easy to customize or filter elements
Method 2: Using dict() with zip()
The zip() function pairs elements from two iterables. Here we zip the list items (as keys) with their indexes (as values).
fruits = ['apple', 'banana', 'cherry']
fruit_dict = dict(zip(fruits, range(len(fruits))))
print(fruit_dict)
Output:
{'apple': 0, 'banana': 1, 'cherry': 2}
This method is easy to modify if you want to start index from a custom number
Method 3: Using a Simple For Loop
fruits = ['apple', 'banana', 'cherry']
fruit_dict = {}
for index in range(len(fruits)):
fruit_dict[fruits[index]] = index
print(fruit_dict)
Output:
{'apple': 0, 'banana': 1, 'cherry': 2}
Method 4: Using map() and lambda
fruits = ['apple', 'banana', 'cherry']
# map each element to (key, value) tuple
fruit_dict = dict(map(lambda i: (i[1], i[0]), enumerate(fruits)))
print(fruit_dict)
Output:
{'apple': 0, 'banana': 1, 'cherry': 2}
Why use this?
> Compact but powerful
> Useful for advanced users who like functional style
You can easily shift the index while using enumerate():
fruits = ['apple', 'banana', 'cherry']
fruit_dict = {value: index for index, value in enumerate(fruits, start=1)}
print(fruit_dict)
Output:
{'apple': 1, 'banana': 2, 'cherry': 3}
FAQs — Convert Python List to Dictionary with Index as Value (context-rich)
How to convert a Python list into a dictionary where each element is the key and its index is the value?
Use a dictionary comprehension with enumerate() to flip value and index:
my_list = ['apple', 'banana', 'cherry']
my_dict = {v: i for i, v in enumerate(my_list)}
# {'apple': 0, 'banana': 1, 'cherry': 2}
Resulting mapping is great for O(1) lookups from value → index.
What is the shortest one-line method to convert a list to a dict with values as keys and indices as values?
The compact one-liner using zip():
dict(zip(my_list, range(len(my_list))))
# {'apple': 0, 'banana': 1, 'cherry': 2}
It pairs each element with its index and builds the dict directly.
How to create the same mapping but start indices from 1 instead of 0 (list→dict with index starting at 1)?
Pass start=1 to enumerate() or shift the range:
{v: i for i, v in enumerate(my_list, start=1)}
# {'apple': 1, 'banana': 2, 'cherry': 3}
How to convert only the first N items of a list into a dict with indices as values?
Slice the list first, then map to indices:
N = 2
dict(zip(my_list[:N], range(N)))
# {'apple': 0, 'banana': 1}
How to handle duplicate elements when converting a Python list to dict where keys are items and values are indices?
Dictionary keys must be unique. If duplicates exist, the last occurrence overwrites earlier ones. To keep all indices, collect lists of indices:
from collections import defaultdict
idxs = defaultdict(list)
for i, v in enumerate(my_list):
idxs[v].append(i)
# idxs => {'a': [0,2], 'b': [1]}
Can I convert nested lists or lists of unhashable items into a dict where the item maps to its index?
Dictionary keys must be hashable. Convert unhashable items (like lists) to tuples first:
nested = [[1,2], [3,4]]
{tuple(v): i for i, v in enumerate(nested)}
# {(1,2): 0, (3,4): 1}
How to skip empty or None items when building a value→index dict from a list?
Filter during comprehension to exclude undesirable values:
{v: i for i, v in enumerate(my_list) if v is not None and v != ''}
# skips None and empty strings
Is mapping list values to indices (dict(enumerate)) fast for large lists?
Yes — enumerate, zip, and dict are implemented in C and operate in O(n) time. They perform well even on large lists (millions of items) but watch memory if both list and dict are kept.
How to use the resulting value→index dict as a lookup table (example use-case)?
Create the mapping and use .get() or direct indexing for O(1) lookups:
lookup = {v: i for i, v in enumerate(my_list)}
index = lookup.get('banana') # returns 1 (or None if missing)