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.

Assume we have taken an input list containing integers. We will now

Methods Used

The following are the various methods used to accomplish this task −

  • Using for loop

  • Using dictionary comprehension and list slicing

Example

Assume we have taken an input list. We will now print alternate list elements as key-value pairs.

Input

inputList = [20, 13, 5, 2, 6, 8, 18, 3]

Output

Dictionary with alternate list elements as key-value pairs:
{13: 2, 2: 8, 8: 3, 20: 5, 5: 6, 6: 18}

Alternate elements are mapped to get key-value pairs. 13 -> 2 [ alternate]

Method 1: Using For Loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store an input list.

  • Print the input list.

  • Create an empty dictionary for storing the resultant dictionary.

  • Use the for loop to traverse till the length of the input list except the last index using range() and len() functions(returns the number of items in an object).

  • The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number.

  • Use the if conditional statement to check whether the current index is even or not with the modulo % operator(returns the remainder).

  • Assign even index elements to the output dictionary as key-value pairs.

  • In the same way, get the other set with odd index elements using the not logical operator for odd index checking.

  • Print the resultant dictionary with alternate list elements as key-value pairs.

Example

The following program returns a dictionary with alternate list elements as key-value pairs using the for loop –

# input list
inputList = [20, 13, 5, 2, 6, 8, 18, 3]

# printing the given input list
print("Input List: ", inputList)

# creating an empty dictionary for storing resultant dict
outputDict = dict()

# =traversing till the length of the input list except for the last index
for i in range(len(inputList) - 2):

   # checking whether the current index is even or not
   if i % 2:
         
      # assigning even index elements to output dict as a key-value pairs
      outputDict[inputList[i]] = inputList[i + 2]

# getting the other set with odd index elements
for i in range(len(inputList) - 2):
   if not i % 2:
      outputDict[inputList[i]] = inputList[i + 2]

# printing the resultant dictionary with alternate list elements as key-value pairs
print("Dictionary with alternate list elements as key-value pairs:\n", outputDict)

Output

On executing, the above program will generate the following output –

Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{13: 2, 2: 8, 8: 3, 20: 5, 5: 6, 6: 18}

Method 2: Using dictionary comprehension and list slicing

Python supports dictionary comprehensions, just like List Comprehension. Dictionaries can be made using straightforward expressions. The formula for dictionary comprehension is −

{key: value for (key, value) in iterable}

List slicing is a frequent practice and the one that programmers utilize the most to solve problems effectively. Consider a Python list. You must slice a list in order to access a range of list elements. The use of the colon(:), a simple slicing operator, is one method for accomplishing this.

Syntax

[start:stop:step]

Parameters

  • start − index from where to start

  • end − ending index

  • step − numbers of jumps to take in between i.e stepsize

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create a variable to store an input list and print the given input list.

  • Use slicing to get all the odd index elements from an input list with a start value of 1, and step value of 2.

  • Use slicing to get all the even index elements from an input list with a start value of 0, and step value of 2.

  • Use dictionary comprehension to get the odd index list elements as key-value pairs by traversing through the odd list

  • Update(add) even index list elements as key-value pairs to the above output Dict.

  • update() function(inserts the given items such as a dictionary, or an iterable object with key-value pairs into the dictionary).

  • Print the resultant dictionary with alternate list elements as key-value pairs.

Example

The following program returns a dictionary with alternate list elements as key-value pairs using dictionary comprehension and list slicing –

# input list
inputList = [20, 13, 5, 2, 6, 8, 18, 3]

# printing the given input list
print("Input List: ", inputList)

# getting all odd index elements from input list using slicing
oddList = inputList[1::2]

# getting all odd index elements from input list using slicing
evenList = inputList[::2]

# getting odd index list elements as key-value pairs
outputDict = {oddList[i]: oddList[i + 1] for i in range(len(oddList) - 1)}

#updating(adding) even index list elements as key-value pairs to the outputDict
outputDict.update({evenList[i]: evenList[i + 1]
for i in range(len(evenList) - 1)})
   
   # printing the resultant dictionary with alternate list elements as key-value pairs
print("Dictionary with alternate list elements as key-value pairs:\n", outputDict)

Output

On executing, the above program will generate the following output –

Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{13: 2, 2: 8, 8: 3, 20: 5, 5: 6, 6: 18}

Conclusion

This article taught us 2 distinct ways to obtain Alternate list elements as key-value pairs. We learned how to use the step values to slice iterables like lists, tuples, etc. Additionally, we learned dictionary comprehension. Finally, we learned how to use the update() function to add or update the dictionary.

Updated on: 27-Jan-2023

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements