如何將Python字典轉換為列表?


在這篇文章中,我們將向您展示如何將 Python 字典轉換為列表。以下是完成此任務的方法

  • 使用 list & items() 方法

  • 使用 keys() 方法

  • 使用 values() 方法

  • 使用列表推導式

  • 使用 zip() 函式

  • 使用 map() 函式

  • 使用 for 迴圈 & items() 方法

字典是 Python 版本的關聯陣列資料結構。字典是鍵值對的集合。每個鍵值對由一個鍵和與其關聯的值表示。

字典由用花括號括起來並用逗號分隔的鍵值對列表定義。每個鍵的值由冒號 (:) 分隔。

字典不能僅僅為了獲得排序字典的表示而進行排序。根據定義,字典是無序的,而其他型別,例如列表和元組,則不是。因此,您需要一種有序的資料型別,即列表——很可能是一個元組列表。

Python 的字典 類為此目的提供了三種方法。items()keys()values() 分別返回包含鍵值對元組、僅鍵和僅值的檢視物件。內建的 list 方法將這些檢視物件轉換為列表物件。

使用 list & items() 方法

Python 的字典類提供items() 函式,該函式返回字典中所有鍵值對 (字典項) 的可迭代序列。此返回的序列表示字典中的實際鍵值對。我們可以使用list() 函式 從此可迭代序列中獲取元組列表。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典。

  • 使用items() 函式獲取字典的所有鍵值對(返回字典中鍵值對的組),並使用 list() 函式(返回可迭代物件的列表)將字典項(鍵值對)轉換為元組列表。

  • 列印轉換後字典的最終列表。

示例

以下程式使用 list & items() 方法將 Python 字典轉換為列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # converting input dictionary items(key-value pair) # to a list of tuples resultList = list(inputDictionary.items()) # printing the resultant list of a dictionary print(resultList)

輸出

執行上述程式後,將生成以下輸出:

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

使用 keys() 方法

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典。

  • 透過將其應用於輸入字典並使用list() 函式(將序列/可迭代物件轉換為列表)將其結果轉換為列表,使用keys() 函式(dict.keys() 方法提供一個檢視物件,按插入順序顯示字典中所有鍵的列表)列印字典所有鍵的列表。

示例

以下程式使用 list() 和 keys() 函式返回字典所有鍵的列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # converting input dictionary keys to a list resultList = list(inputDictionary.keys()) # printing the resultant list of a dictionary keys print(resultList)

輸出

執行上述程式後,將生成以下輸出:

['Hello', 'Tutorialspoint', 'python']

使用 values() 方法

要獲得值列表,請使用字典的values() 方法。我們可以使用list() 函式將值轉換為列表。

示例

以下程式使用 list() 和 values() 函式返回字典所有值的列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # converting input dictionary values to a list resultList = list(inputDictionary.values()) # printing the resultant list of a dictionary values print(resultList)

輸出

執行上述程式後,將生成以下輸出:

[10, 20, 30]

使用列表推導式

列表推導式使在 Python 中建立列表變得簡單。此外,您可以在同一行更改元素的值。

使用dictionary.items() 在將它們新增到列表之前檢索項的鍵和值。

示例

以下程式使用列表推導式將 Python 字典轉換為列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # Storing key and value as list of tuples using list comprehension resultList = [(key, value) for key, value in inputDictionary.items()] # printing the resultant list of a dictionary key-values print(resultList)

輸出

執行上述程式後,將生成以下輸出:

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

使用 zip() 函式

zip() 函式可用於組合兩個列表/迭代器。透過這種方式,我們可以組合字典的 .keys() 和 .values() 方法以同時訪問兩個值。

示例

以下程式使用 list zip() 函式將 Python 字典轉換為列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} #combining keys and values of dictionary using zip() function resultList = zip(inputDictionary.keys(), inputDictionary.values()) # converting zip object to list resultList = list(resultList) # printing the resultant list of a dictionary key-values print(resultList)

輸出

執行上述程式後,將生成以下輸出:

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

使用 map() 函式

map() 是一個內建函式,允許您將任何函式對映到迭代器/列表上。要建立列表的列表,請將 list() 函式對映到dictionary.items() 的所有元素上。

示例

以下程式使用 list map() 函式將 Python 字典轉換為列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # conveting list of keys and values of dictionary to a list using map() function resultList = new_list = list(map(list, inputDictionary.items())) # printing the resultant list of a dictionary key-values print(resultList)

輸出

執行上述程式後,將生成以下輸出:

[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]

使用 for 迴圈 & items() 方法

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典。

  • 建立一個空列表,該列表給出字典鍵、值的最終列表。

  • 使用for 迴圈 使用items() 函式(返回字典中鍵值對的組)遍歷字典的每個鍵值對。

  • 使用append() 函式(將元素新增到列表的末尾)將相應的鍵值對列表附加到列表。

  • 列印字典鍵值的最終列表

示例

以下程式使用 for 迴圈、append()、items() 函式將 Python 字典轉換為列表:

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # creating an empty list resultList = [] # traversing through each key value pair of a dictionary using items() function for key, val in inputDictionary.items(): # appending list of corresponding key-value pair to a list resultList.append([key, val]) # printing the resultant list of a dictionary key-values print(resultList)

輸出

執行上述程式後,將生成以下輸出:

[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]

結論

在這篇文章中,我們學習瞭如何使用七種不同的方法將給定的字典轉換為列表。我們還學習瞭如何使用 items() 函式迭代字典。

更新於:2023年8月23日

102K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告