Open In App

Convert Each List Element to Key-Value Pair – Python

Last Updated : 24 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list we need to convert it into the key- value pair. For example we are given a list li = [‘apple’, ‘banana’, ‘orange’] we need to convert it to key value pair so that the output should be like {1: ‘apple’, 2: ‘banana’, 3: ‘orange’}. We can achieve this by using multiple methods like enumerate, defaultdict and various approaches using loops.

Using enumerate()

enumerate() generates an iterator that yields tuples of (index, value) for each element in an iterable start parameter allows you to specify the starting index for the enumeration, which defaults to 0.

Python
li = ['apple', 'banana', 'orange']
a = dict(enumerate(li, 1)) 
print(a) 

Output
{1: 'apple', 2: 'banana', 3: 'orange'}

Explanation:

  • enumerate(list1, 1) creates an iterator that generates tuples of (index, value) for each element in the list starting the index from 1.
  • dict() converts the iterator of tuples into a dictionary, where the index becomes the key and the value remains the same.

Using a Loop

Using a loop, initialize an empty dictionary and iterate over the list with enumerate, adding each index as a key and its corresponding element as the value.

Python
li = ["apple", "banana", "cherry"]

# Initialize an empty dictionary
res = {}

# Iterate through the list with indices using enumerate
for index, value in enumerate(li):
    res[index] = value  # Add index as key and element as value

print(res)

Output
{0: 'apple', 1: 'banana', 2: 'cherry'}

Explanation:

  • An empty dictionary res is created, and the enumerate function is used to loop through the list li, providing both the index and the element of each item.
  • During each iteration, the index is added as a key and the corresponding element as the value in the dictionary res.

Using zip

Use zip to pair indices from range(len(li)) with elements from li, creating key-value pairs. Convert the zipped object into a dictionary using dict.

Python
li = ["apple", "banana", "cherry"]

# Create a dictionary by zipping a range of indices with the elements
res = dict(zip(range(len(li)), li))

print(res)

Output
{0: 'apple', 1: 'banana', 2: 'cherry'}

Explanation:

  • Zip pairs the indices (generated by range(len(li))) with the corresponding elements from the list li.
  • Zip object is converted into a dictionary using dict(), where indices become keys and elements become values.

Using defaultdict

Use defaultdict from the collections module to initialize a dictionary with default values. Then by enumerating the list, where indices are the keys and elements are the values.

Python
from collections import defaultdict

li = ["apple", "banana", "cherry"]

# Use defaultdict to create a dictionary where missing keys default to a string
res = defaultdict(str, enumerate(li))

print(dict(res))  # Convert defaultdict to a regular dictionary for display

Output
{0: 'apple', 1: 'banana', 2: 'cherry'}

Explanation:

  • defaultdict(str) creates a dictionary that automatically assigns an empty string as the default value for missing keys.
  • enumerate(li) pairs indices with elements from the list li and defaultdict stores them as key-value pairs, with indices as keys and elements as values.


Next Article

Similar Reads