How to zip a Python Dictionary and List together?

PythonServer Side ProgrammingProgramming

In this article, we will show you how to zip a python dictionary and list together. Below are the various methods to accomplish this task −

  • Using zip() function

  • Using append() function and items() function

  • Using append() function and in operator.

Combing dictionary and list together using zip()

Algorithm (Steps)

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

  • Create a variable to store the input dictionary.

  • Create another variable to store the input list.

  • Use the zip() function(The zip() function can be used to combine two lists/iterators) to combine the input dictionary and list together by passing the key-value pairs of a dictionary using the items() function(returns the key-value pairs of a dictionary) and input list as arguments to it.

  • Here it returns the result as a zip object of the given dictionary and input list.

  • Print the resultant zip result by converting the above zip object into the list using the list() function(returns a list of an iteratable).

Example

The following program combines the input dictionary and list together using the zip() function −

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # combining input dictionary and list together zippedResult = zip(inputDict.items(), inputList) print("Combining input dictionary and list together:") # printing the resultant zip result by converting the # above zip object into a list print(list(zippedResult))

Output

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

Combining input dictionary and list together:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]

Creating a Zip Object and Accessing it

Algorithm (Steps)

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

  • Use the for loop to traverse through the key, and value pair of the input dictionary and list items in the above zip object

  • Print the corresponding key of a dictionary.

  • Print the corresponding value of a dictionary.

  • Print the corresponding list item of an input list.

Example

The following program combines the input dictionary and list together using zip() function and prints the key, the value of the dictionary, and list item of the zipped object −

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # combining input dictionary and list together zippedObject = zip(inputDict.items(), inputList) print("Zipping input dictionary and list:") # traversing through key, value pair of dictionary and list items in # the above zip object for (k, v), listItem in zippedObject: # printing the corresponding key of a dictionary print(k) # printing the corresponding value of a dictionary print(v) # printing corresponding list item of the input list print(listItem)

Output

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

Zipping input dictionary and list:
Hello
1
10
tutorialspoint
2
20
python
3
30
codes
3
40

Using the append() and items() functions

Algorithm (Steps)

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

  • Take an empty list to store the zip object of the given dictionary and list.

  • Take an index value of the list and initialize its value to 0.

  • Traverse through the key−value pairs of the dictionary using the items() function.

  • Create a tuple of the key and value of the dictionary and store it in a variable.

  • Append the above tuple and list element at the current index to the list as a tuple using the append() function.

  • Increment the value of the list index by 1.

  • Print the zip Object of the dictionary and list.

Example

The following program combines the input dictionary and list together using the append() and items() functions −

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # empty list for storing result result = [] # initializing a variable of list index with 0 i=0 # traversing through key, value pair of input dictionary for key,value in inputDict.items(): # creating a tuple of key-value pairs of a dictionary dict_key_value_tuple = (key,value) # appending corresponding key, value pair of input dictionary, and input list element to the above result list result.append(((dict_key_value_tuple),inputList[i])) # incrementing the value of the list index by 1 i+=1 # printing the resultant zip of input dictionary and list print("Zipping input dictionary and list:
"
, result)

Output

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

Zipping input dictionary and list:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]

Using the append() function and in operator

Example

The following program combines the input dictionary and list together using the append() function and in operator −

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # empty list for storing result result = [] # initializing a variable of list index with 0 i=0 # traversing through key of input dictionary for key in inputDict: # creating a tuple of key-value pair of a dictionary # here inputDict[key] represents value of a dictionary dict_key_value_tuple = (key,inputDict[key]) # appending corresponding key, value pair of input dictionary, # input list element as tuple to the above result list result.append(((dict_key_value_tuple),inputList[i])) # incrementing the value of the list index by 1 i+=1 # printing the resultant zip of input dictionary and list print("Zipping input dictionary and list:
"
, result)

Output

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

Zipping input dictionary and list:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]

Conclusion

In this article, we studied three techniques for zipping a given dictionary or list in Python. We learned how to access the zip object and display its values. We also learned how to iterate through the dictionary using the items() function and the in operator.

raja
Updated on 09-Nov-2022 07:04:11

Advertisements