Python - Alternate list elements as key-value pairs Last Updated : 14 Oct, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 # Python3 code to demonstrate working of # Alternate Elements Dictionary # Using loop # initializing list test_list = [2, 3, 5, 6, 7, 8, 9, 10] # printing original list print("The original list is : " + str(test_list)) res = dict() # pairing first set of Alternate elements for idx in range(len(test_list) - 2): if idx % 2: res[test_list[idx]] = test_list[idx + 2] # pairing other set for idx in range(len(test_list) - 2): if not idx % 2: res[test_list[idx]] = test_list[idx + 2] # printing result print("The extracted dictionary : " + str(res)) OutputThe 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 # Python3 code to demonstrate working of # Alternate Elements Dictionary # Using dictionary comprehension + list slicing # initializing list test_list = [2, 3, 5, 6, 7, 8, 9, 10] # printing original list print("The original list is : " + str(test_list)) # extracting lists list1 = test_list[1::2] list2 = test_list[::2] # constructing pairs using dictionary comprehension 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)}) # printing result print("The extracted dictionary : " + str(res)) OutputThe 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} Comment More infoAdvertise with us Next Article Python - Alternate Default Key Value M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Convert Each List Element to Key-Value Pair - Python 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 3 min read Python - Alternate Default Key Value Sometimes, while working with Python Dictionaries, we can have problem in which we need to assign a particular value to a particular key, but in absence, require the similar's key's value but from different dictionary. This problem can have applications in domain of web development. Let's discuss ce 6 min read Python - Alternate Minimum element in list Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications and sometime 4 min read Python - Alternate Elements operation on Tuple Sometimes, while working with Python Tuples, we can have problem in which we need to perform operations of extracted alternate chains of tuples. This kind of operation can have application in many domains such as web development. Lets discuss certain ways in which this task can be performed. Input : 5 min read Python - Assign pair elements from Tuple Lists Given a tuple list, assign each element, its pair elements from other similar pairs. Input : test_list = [(5, 3), (7, 5), (8, 4)] Output : {5: [3], 7: [5], 8: [4], 4: []} Explanation : 1st elements are paired with respective 2nd elements from all tuples. Input : test_list = [(5, 3)] Output : {5: [3] 7 min read Python - List consisting of all the alternate elements We are given a list consisting we need to get all alternate elements of that list. For example, we are given a list a = [1, 2, 3, 4, 5, 6, 7, 8] we need to get all alternate elements of that list so that output should be [1, 3, 5, 7].Using List SlicingList slicing allows us to select every alternate 2 min read Like