Python – Alternate list elements as key-value pairs
Last Updated :
14 Oct, 2020
Given a list, convert it into dictionary by mapping alternate elements as key-value pairs.
Input : test_list = [2, 3, 5, 6, 7, 8]
Output : {3: 6, 6: 8, 2: 5, 5: 7}
Explanation : Alternate elements mapped to get key-value pairs. 3 -> 6 [ alternate]
Input : test_list = [2, 3, 5, 6]
Output : {3: 6, 2: 5}
Explanation : Alternate elements mapped to get key-value pairs. 3 -> 6 [ alternate]
Method #1 : Using loop
This is one of the ways in which this task can be performed. In this, we iterate twice to get both the alternate elements to get desired all alternate key-value dictionary.
Python3
test_list = [ 2 , 3 , 5 , 6 , 7 , 8 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
res = dict ()
for idx in range ( len (test_list) - 2 ):
if idx % 2 :
res[test_list[idx]] = test_list[idx + 2 ]
for idx in range ( len (test_list) - 2 ):
if not idx % 2 :
res[test_list[idx]] = test_list[idx + 2 ]
print ( "The extracted dictionary : " + str (res))
|
Output
The original list is : [2, 3, 5, 6, 7, 8, 9, 10]
The extracted dictionary : {3: 6, 6: 8, 8: 10, 2: 5, 5: 7, 7: 9}
Method #2 : Using dictionary comprehension + list slicing
This is yet another way in which this task can be performed. In this, we slice out both alternate elements lists and use dictionary comprehension for pairing.
Python3
test_list = [ 2 , 3 , 5 , 6 , 7 , 8 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
list1 = test_list[ 1 :: 2 ]
list2 = test_list[:: 2 ]
res = {list1[idx] : list1[idx + 1 ] for idx in range ( len (list1) - 1 )}
res.update({list2[idx] : list2[idx + 1 ] for idx in range ( len (list2) - 1 )})
print ( "The extracted dictionary : " + str (res))
|
Output
The original list is : [2, 3, 5, 6, 7, 8, 9, 10]
The extracted dictionary : {3: 6, 6: 8, 8: 10, 2: 5, 5: 7, 7: 9}
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...