Iread Pages

Main Menu

  • Home
  • Magazine
  • Ebook
  • Dictionary
  • Novel

Iread Pages

Header Banner

Iread Pages

  • Home
  • Magazine
  • Ebook
  • Dictionary
  • Novel
Dictionary
Home›Dictionary›How to convert Python dictionary to list

How to convert Python dictionary to list

By Katrina G. Dibiase
May 25, 2022
0
0

Data manipulation happens a lot in programming, depending on the problem you intend to solve. You may often find yourself converting one data structure to another. Some types are so similar that converting between them is a straightforward process.

In Python, turning a dictionary into a nested or flattened list is a popular conversion you’ll find yourself performing.

USE VIDEO OF THE DAY

Convert a Python dictionary to a list using a for loop

The for loop gives you more traction on your data while converting a Python dictionary to a list.

For example, the following code converts a dictionary to a nested list:

myDictionary = {"A": "Python", "B": "JavaScript", "C": "Node"}
convertedList = []

for i in myDictionary:
convertedList.append([i, myDictionary[i]])

print(convertedList)

The above code inserts each key (I) and the value (My dictionary[i]) pair in individual lists and appends them to an empty list.

This is the same as writing:

for key, value in myDictionary.items():
convertedList.append([key, value])

You can also place each pair in a set or a tuple. You just need to replace the square braces ([]) around key value associate with braces ({}) or parenthesis (()) Consequently.

You can also achieve this by using a for loop with Python’s list comprehension function:

convertedList = [[i, myDictionary[i]] for i in myDictionary]

Function to convert a Python dictionary to a flat list

While the above for loop options produce a nested list, you can break it down further into a simple list if that’s what you want.

The following function does this:

def convertToStraightList(sampleDict):
flatList = []

for key, value in sampleDict.items():
flatList.extend([key, value])

return flatList

print(convertToStraightList(myDictionary))

The above function returns a flattened list and the concept is simple. The loop adds each key and assess pair to a list that the function returns when finished.

Using Built-in One-Liner Features

Both map and Zip*: French allow one-line Python solutions to this problem, with different results. They may be more suitable than for loop, depending on your problem, and they are certainly more convenient.

The Zip*: French The function produces a nested list of tuples:

convertedList = list(zip(myDictionary.keys(), myDictionary.values()))
print(convertedList)

The map function, on the other hand, returns a list of lists:

convertedList = list(map(list, myDictionary.items()))
print(convertedList)

Convert between Python lists and dictionaries back and forth

These different ways to convert a dictionary to a list are quite simple in Python. So you can turn a dictionary into a list, but you can also do the opposite by turning a Python list into a dictionary.

Related posts:

  1. UWC to develop the first Kaaps dictionary
  2. Why use a dictionary in the age of Internet research?
  3. In a nutshell: How to insert your new word in the dictionary
  4. Dictionary.com trolls Mike Richards with the definition of “jeopardy” after his resignation

Categories

  • Dictionary
  • Ebook
  • Magazine
  • Novel

Recent Posts

  • Irritability and new treatments for key symptoms most dissatisfied with autism
  • What is @#$%! Everett’s mom publishes swear dictionary
  • Tony Jeton Selimi’s The Unfakeable Code® Wins Top Shelf Magazine Book Awards 2022 in Four Categories
  • Athol Daily News – Ask the PC Doctor: Can I turn an ebook into an audiobook?
  • eBook: Unlock Complex, Streaming Data with Declarative Data Pipelines

Archives

  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • July 2020
  • June 2020
  • January 2020
  • February 2019
  • September 2017
  • Privacy Policy
  • Terms and Conditions