Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Alternate list elements as key-value pairs
In this article, we will learn how to get alternate list elements as key-value pairs in Python. This technique is useful when you need to transform a list into a dictionary by pairing elements at alternate positions.
Assume we have an input list containing integers. We need to create key-value pairs where each element is paired with the element that appears two positions ahead ?
Methods Used
The following are the various methods used to accomplish this task ?
Using for loop
Using dictionary comprehension and list slicing
Example Overview
Input
input_list = [20, 13, 5, 2, 6, 8, 18, 3]
print("Input List:", input_list)
Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Expected Output
Dictionary with alternate list elements as key-value pairs:
{20: 5, 13: 2, 5: 6, 2: 8, 6: 18, 8: 3}
Each element is paired with the element two positions ahead: 20 ? 5, 13 ? 2, and so on.
Method 1: Using For Loop
This method iterates through the list and creates key-value pairs by pairing each element with the element two positions ahead ?
# input list
input_list = [20, 13, 5, 2, 6, 8, 18, 3]
# printing the given input list
print("Input List:", input_list)
# creating an empty dictionary for storing resultant dict
output_dict = {}
# traversing till the length of the input list minus 2
for i in range(len(input_list) - 2):
# pairing current element with element at i+2 position
output_dict[input_list[i]] = input_list[i + 2]
# printing the resultant dictionary
print("Dictionary with alternate list elements as key-value pairs:")
print(output_dict)
Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{20: 5, 13: 2, 5: 6, 2: 8, 6: 18, 8: 3}
Method 2: Using Dictionary Comprehension and List Slicing
This method uses dictionary comprehension for a more concise solution. List slicing allows us to access ranges of elements with specific step sizes ?
List Slicing Syntax
[start:stop:step]
start ? index from where to start
stop ? ending index (exclusive)
step ? number of positions to skip
# input list
input_list = [20, 13, 5, 2, 6, 8, 18, 3]
# printing the given input list
print("Input List:", input_list)
# using dictionary comprehension to create key-value pairs
# pairing each element with the element two positions ahead
output_dict = {input_list[i]: input_list[i + 2] for i in range(len(input_list) - 2)}
# printing the resultant dictionary
print("Dictionary with alternate list elements as key-value pairs:")
print(output_dict)
Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{20: 5, 13: 2, 5: 6, 2: 8, 6: 18, 8: 3}
Alternative Approach: Using zip() Function
The zip() function provides another elegant solution by pairing elements from two slices of the list ?
# input list
input_list = [20, 13, 5, 2, 6, 8, 18, 3]
# printing the given input list
print("Input List:", input_list)
# using zip to pair elements
output_dict = dict(zip(input_list[:-2], input_list[2:]))
# printing the resultant dictionary
print("Dictionary with alternate list elements as key-value pairs:")
print(output_dict)
Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{20: 5, 13: 2, 5: 6, 2: 8, 6: 18, 8: 3}
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For Loop | High | Good | Beginners, complex logic |
| Dictionary Comprehension | Medium | Better | Concise code |
| zip() Function | High | Best | Clean, Pythonic solution |
Conclusion
This article demonstrated three methods to create key-value pairs from alternate list elements. Dictionary comprehension and the zip() function provide more Pythonic solutions, while the for loop approach offers better readability for beginners.
