Python dict items to list

How to convert Python Dictionary to a list [9 Different ways]

In this Python tutorial, we will understand how Python convert dictionary to list using examples.

In Python, we can convert dictionary to list in Python using the following 9 methods.

  1. Using items() Method
  2. Using keys() Method
  3. Using values() Method
  4. Using loop + items() Method
  5. Using List Comprehension Method
  6. Using Zip Function
  7. Using Iteration Method
  8. Using Collection Method
  9. Using Map Function

Python convert dictionary to list

Here we will cover all 9 methods to convert dictionary to list in Python with various examples.

Method 1: By using items() method

In this section, we will understand how to use the items() method to convert a dictionary to a list in Python.

The dict.items() method is used to iterate the elements of the dictionary. Using list() we will get all the items converted into the list.

my_dict = con_lis = list(my_dict.items()) print("Converted dict to list:",con_lis)
  • In the above code first, we will create a dictionary by using curly brackets and assign them key-values pairs.
  • The con_lis variable needs to be converted into a list so that we can use the list() function and then store the result in the con_lis variable.

Python convert dictionary to list

Method 2: By using values() method

The dict.values() method in Python can easily get a list of values and to convert values to a list, we can use the list() function

Let’s take an example and check how to convert a dictionary to a list

to_dict = new_val = list(to_dict.values()) print("Converted dict to list:",new_val)

Here is the Screenshot of the following given code

Python convert dictionary to list values method

Method 3: By using zip() function

In this example, we can combine the zip(), keys(), and values() methods to convert a dictionary into a list.

To create a list of tuples from a Python Dictionary, we can use the zip() function. The zip() function is mostly used in Python for combining two iterators.

Here is the sample code in Python.

to_dict = m = list(zip(to_dict.keys(), to_dict.values())) print("Convert dictionary to list:",m)

Here is the implementation of the following given code

Python convert dictionary to list zip method

Method 4: By using keys() method

The keys() method in Python is used to get the list of keys from the dictionary. To get the list of all keys, we can combine the list() function with the keys() method.

Let us look at an example of using methods in Python.

new_dict = n_val = list(new_dict.keys()) print("Convert dictionary to list",n_val)

Python convert dictionary to list keys method

Method 5: By using list comprehension method

In this method, we will dict.items() method to collect the keys and values from a dictionary. After this, we will use the list comprehension method to form a list out of it.

Читайте также:  Internal error java io ioexception cannot find intellij idea project files at

Here is an example of this implementation in Python.

n_dictionary = n_lis = [(key, value) for key, value in n_dictionary.items()] print(n_lis)

Check the execution of the following given code

Python convert dictionary to list comprehension method

Method 6: By using map() function

The map function will take a function and a sequence as a parameter and it applies that function to the iterable and returns a new list.

In this example, you can combine the map() and list() functions to convert the dictionary into a list.

Source code:

my_dict = con_val= list(map(list,my_dict.items())) print(con_val)

Python convert dictionary to list map function

This is how to convert a dictionary to a list.

Method 7: Using for loop & items() Method

In this method, we will understand the use of for loop and dict.items() method to convert a dictionary to a list in Python

Here, we will first use the for loop to iterate over each item in the dictionary. And then we used the list() function to convert items into a list.

Here is a sample example of this execution in Python.

# Defining a dictionary User = < "Name":"James", "Age":25, "City": "New York", "Country": "United States" ># Defining a list user_data = [] # Using for loop & items() for key, val in User.items(): user_data.append(Python dict items to list) # Printing list print(user_data)
  • In the above example, we defined a dictionary named user which consists of multiple key-value pairs.
  • After this, we defined an empty dictionary named user_data. Next, we utilized the for loop and item() method to fetch each key value from the dictionary and append it to the new list.

Here is the final execution of the above Python program.

Python Convert Dictionary to List using loop and items

Method 8: Using Iteration Method

In this section, we will understand how to use iteration and the list() function in Python to convert a dictionary to a list.

The example for the execution of the above Python program is given below.

# Defining a dictionary User = < "Name":"James", "Age":25, "City": "New York", "Country": "United States" ># Defining a list user_data = [] # Using iteration for item in User: k = [item, User[item]] user_data.append(k) # Printing list  print(user_data)

In the above Python program, we utilized for loop to iterate over each item and used square brackets ([]) to have each key-value pair as a list. In the last, we used the list.append() method to append each key-value pair list into the list.

Here is the final result of the above example.

Python Convert Dictionary to List using iteration

Method 9: Using collection.namedtuple()

In this method, we will discuss how to convert dictionary to list using collection.namedtuple().

The collection.namedtuple() is a subclass in the Python collection module.

This subclass enables us to define new tuple types, each of which has a named field with a certain type. However, these named tuples are also immutable just like standard tuples in Python.

In this approach, we will convert dictionary key-value pair to named tuple using collection.namedtuple() class. After this, we will store these named tuples into a list.

The example for this approach in Python is given below.

from collections import namedtuple # Defining a dictionary User = < "Name":"James", "Age":25, "City": "New York", "Country": "United States" >list_of_tuple = namedtuple('List', 'name value') new_list = list(list_of_tuple(*item) for item in User.items()) # Printing list print(new_list)

In the example, we are converting the User dictionary to new_list list type value using collection.namedtuple() in Python.

Here is the final result of the above Python program.

Читайте также:  Java check status code

Python Convert Dictionary to List using collection

Conclusion

So, in this Python tutorial, we understood how to Python convert dictionary to list using the following 9 methods.

  1. Using items() Method
  2. Using keys() Method
  3. Using values() Method
  4. Using loop + items() Method
  5. Using List Comprehension Method
  6. Using Zip Function
  7. Using Iteration Method
  8. Using Collection Method
  9. Using Map Function

You may also like reading the following articles.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

5 Easy ways to convert a dictionary to a list in Python

Top 5 Ways To Convert Dictionary To List In Python

In this tutorial, we are going to discuss the top five ways to convert a Python dictionary to a Python list. So, let’s get started!

5 Methods to Convert a Dictionary to a list

Let’s now learn how you can convert a dictionary to a list in Python. In the next few sections, you’ll see various methods and functions that are used in Python to change the datatype from dictionary to list. Let’s get started!

Method 1: Using dict.items() function

In this method, we will be using the dict.items() function of dictionary class to convert a dictionary to a list. The dict.items() function is used to iterate over the key: value pairs of the Python dictionary. By default, the key: value pairs are stored in the form of a Python tuple in the iterator returned by the dict.items() function. We can pass this returned iterator to the list() function which will finally give us a Python list of tuples containing the key: value pairs of the given Python dictionary. Let’s get into the actual Python code to implement this method.

# Method- 1 # Create a Python dictionary dr = print('Given Python dictionary:\n') print(dr) # Convert the above Python dictionary to a Python list # Using the dict.items() function ls = list(dr.items()) print('\nThis is the converted list:\n') print(ls)
Given Python dictionary: This is the converted list: [('AskPython', 'Python'), ('LinuxForDevices', 'Linux'), ('QuickExcel', 'Excel')]

Method 2: Using zip() function

In this method, we will use the zip() function in Python to convert a dictionary to a list. The zip() function in Python is used to combine two iterator objects by zipping their values.

Here we will pass over two Python lists as the two iterator objects, one will be the list of all the keys of the given Python dictionary and the other will be the list of all the values of the given Python dictionary. We will get these two lists using the dict.keys() and the dict.values() functions of the Python dictionary class. By default, the key: value pairs are stored in the form of a Python tuple in the iterator returned by the zip() function. We can pass this returned iterator to the list() function which will finally return a Python list of tuples containing all the key: value pairs of the given Python dictionary. Let’s see how to implement this method through Python code.

# Method- 2 # Create a Python dictionary dr = print('Given Python dictionary:\n') print(dr) # Convert the above Python dictionary to a Python list # Using the zip() function iter = zip(dr.keys(), dr.values()) ls = list(iter) print('\nThis is the Converted list:\n') print(ls)
Given Python dictionary: This is the Converted list: [('Python', '.py'), ('C++', '.cpp'), ('Csharp', '.cs')]

Method 3: Using map() function

In this method, we will use the map() function in Python to convert a dictionary to a list in Python. The map() function in Python is used to map any Python function over an iterator object.

Читайте также:  Simple Map

Here we will pass the Python list() function as the first argument, and the iterator object returned by the dict.item() function as the second argument. Then we can pass the iterator returned by the map() function to the list() function which will return a Python list of lists containing all the key: value pairs of the given Python dictionary. Let’s write the Python code to implement this method.

# Method- 3 # Create a Python dictionary dr = print('Given Python dictionary:\n') print(dr) # Convert the above Python dictionary to a Python list # Using the map() function iter = map(list, dr.items()) ls = list(iter) print('\nThis is the Converted list:\n') print(ls)
Given Python dictionary: This is the Converted list: [['Amazon', 'AWS'], ['Microsoft', 'AZURE'], ['Google', 'GCP']]

Method 4: Using simple for loop iteration

In this method, we will use a simple for loop to convert a dictionary to a list. We will first create an empty list, then iterate over the keys of the given Python dictionary using the for loop. Finally, we will create one Python tuple for each key: value pair of the given Python dictionary as we iterate over it and append this tuple to the empty list, we created. Let’s implement this method through Python code.

# Method- 4 # Create a Python dictionary dr = print('Given Python dictionary:\n') print(dr) # Create an empty Python list ls = [] # Convert the above Python dictionary to a Python list # Using the for loop iteration for key in dr: item = (key, drPython dict items to list) ls.append(item) print('\nThis is the Converted list:\n') print(ls)
Given Python dictionary: This is the Converted list: [('Sanjay', 'ECE'), ('Abhishek', 'EE'), ('Sarthak', 'ICE')]

Method 5: Using list comprehension

The list comprehension method is the most widely used and compact way to initialize a Python list. We can also provide the list elements in the same single line using a for loop. Here we will fetch the key: value pairs of the given Python dictionary using a for loop and dict.items() function. Then we will initialize the Python list with the fetched key: value pairs of the given Python dictionary. Let’s see how we can use this method to convert a given Python dictionary to a Python list.

# Method- 4 # Create a Python dictionary dr = print('Given Python dictionary:\n') print(dr) # Convert the above Python dictionary to a Python list # Using the list comprehension ls = [(key, value) for key, value in dr.items()] print('\nThis is the Converted list:\n') print(ls)
Given Python dictionary: This is the Converted list: [('A', 65), ('B', 66), ('C', 67), ('D', 68)]

Summing-up

In this tutorial, we have learned the top five ways to convert a dictionary to a list in Python. Hope you have understood the things and ready to experiment with these methods. Thanks for reading this article and stay tuned with us for more such exciting learning content for Python programming.

Источник

Оцените статью