
- 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
Convert dictionary to list of tuples in Python
Converting from one collection type to another is very common in python. Depending the data processing needs we may have to convert the key value pairs present in a dictionary to pairs representing tuples in a list. In this article we will see the approaches to achieve this.
With in
This is a straight forward approach where we just consider the
Example
Adict = {30:'Mon',11:'Tue',19:'Fri'} # Given dictionary print("The given dictionary: ",Adict) # Using in Alist = [(key, val) for key, val in Adict.items()] # Result print("The list of tuples: ",Alist)
Output
Running the above code gives us the following result −
The given dictionary: {30: 'Mon', 11: 'Tue', 19: 'Fri'} The list of tuples: [(30, 'Mon'), (11, 'Tue'), (19, 'Fri')]
With zip
The zip function combines the items passed onto it as parameters. So we take the keys and values of the dictionary as parameters to the zip function and put the result under a list function. The key value pair become tuples of the list.
Example
Adict = {30:'Mon',11:'Tue',19:'Fri'} # Given dictionary print("The given dictionary: ",Adict) # Using zip Alist = list(zip(Adict.keys(), Adict.values())) # Result print("The list of tuples: ",Alist)
Output
Running the above code gives us the following result −
The given dictionary: {30: 'Mon', 11: 'Tue', 19: 'Fri'} The list of tuples: [(30, 'Mon'), (11, 'Tue'), (19, 'Fri')]
With append
In this approach we take a empty list and append evry pair of key value as tuples. A for loop is designed to convert the key value pair into tuples .
Example
Adict = {30:'Mon',11:'Tue',19:'Fri'} # Given dictionary print("The given dictionary: ",Adict) Alist = [] # Uisng append for x in Adict: k = (x, Adict[x]) Alist.append(k) # Result print("The list of tuples: ",Alist)
Output
Running the above code gives us the following result −
The given dictionary: {30: 'Mon', 11: 'Tue', 19: 'Fri'} The list of tuples: [(30, 'Mon'), (11, 'Tue'), (19, 'Fri')]
- Related Articles
- Python program to convert a list of tuples into Dictionary
- List of tuples to dictionary conversion in Python
- Convert list of tuples to list of list in Python
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Convert list of tuples into list in Python
- Convert list of tuples into digits in Python
- How to convert list to dictionary in Python?
- Python program to convert elements in a list of Tuples to Float
- How to convert Python Dictionary to a list?
- Convert key-values list to flat dictionary in Python
- Python – Convert List to Index and Value dictionary
- Python program to Convert a elements in a list of Tuples to Float
- Python program to Convert Matrix to Dictionary Value List
- Combining tuples in list of tuples in Python
