
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
- Related Articles
- Python Program to print key value pairs in a dictionary
- Python – Check alternate peak elements in List
- List consisting of all the alternate elements in Python
- Java Program to create a HashMap and add key-value pairs
- Python program to count pairs for consecutive elements
- How to extract subset of key-value pairs from Python dictionary object?
- Java Program to copy all the key-value pairs from one Map into another
- Updating a set of documents from a list of key value pairs in MongoDB
- Alternate range slicing in list (Python)
- Alternate element summation in list (Python)
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- Alternate Key in RDBMS
- Python - Clearing list as dictionary value
- Python program to add elements to a list
- How to add key/value pairs in SortedList in C#?
