在Python中將字典新增到列表


字典允許我們儲存鍵值對,而列表允許你將多個值儲存在一個變數中,一個常見的任務是將字典新增到列表中,這對於將多個字典儲存在一個列表中非常有用。在本文中,我們將探討如何使用不同的方法在Python中將字典新增到列表中。我們將提供示例和分步說明。

引言

Python提供了各種內建資料結構,使程式設計簡單有效。與其他資料結構相比,這些Python資料結構中的每一個都遵循順序模式,並且可以以多種形式儲存資料集合。Python有許多資料結構,其中一些是基本的但功能強大的,可以儲存各種各樣的資料,例如列表和字典。

列表

Python列表與其他程式語言中指定的動態縮放陣列相同(C++中的向量和Java中的ArrayList)。Python列表可以包含任意數量的元素。用方括號 [] 括起來,並用逗號分隔的一組專案被稱為列表。

可以使用列表將資料集合儲存為序列的形式,列表是一種序列資料型別。還有其他稱為元組和字串的序列型別的資料型別。

語法

List= [enter the elements you want in the list]

元素應該用雙引號括起來,並用逗號分隔。

示例

sample_list1 = ["List","Example","In","Python"]
print(sample_list1)

輸出

['List', 'Example', 'In', 'Python']

字典

Python字典可以被認為是一個有序的元素集合(從Python 3.7開始)。專案被儲存為鍵值對。在這種情況下,鍵表示與每個值關聯的唯一識別符號。

將鍵值對列表放在花括號 {} 中是一種建立字典的方法。列表應該用逗號分隔。每個鍵都用冒號 (:) 與其關聯的值區分開來。

語法

Dict1={<key>: <Value>,…………,………}

示例

sports = {"1": "Cricket", "2": "Volleyball", "3": "Football"}
print(sports)

輸出

{'1': 'Cricket', '2': 'Volleyball', '3': 'Football'}

在Python中將字典新增到列表

以下是我們可以使用的一些方法,可以在Python中將字典新增到列表中:

使用append()方法

我們將使用append函式將字典新增到列表中。

語法

List=list1.append(dictionary)

示例

#list
l1 = [6, 24, "hello", "there"]

# dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city)

# display list
l1

輸出

[6,
 24,
 'hello',
 'there',
 {2024: 'Delhi',
  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'}]

使用append()和copy()方法

我們將使用copy函式建立字典的副本,然後使用append函式將字典新增到列表中。

語法

List=list1.append(dictionary.copy())

示例

#list
l1 = [6, 24, "hello", "there"]

# dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city.copy())

# display list
l1

輸出

[6,
 24,
 'hello',
 'there',
 {2024: 'Delhi',
  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'}

使用append()和deepcopy()方法

在這裡,我們將使用deepcopy()函式遞迴地建立字典的副本,然後使用append函式將字典新增到列表中。

語法

List=list1.append(dictionary.deepcopy())

示例

#list
l1 = [6, 24, "hello", "there"]

# dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city.deepcopy())

# display list
l1

輸出

[6,
 24,
 'hello',
 'there',
 {2024: 'Delhi',
  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'}

使用append()方法和空列表

我們將使用append函式將字典新增到空列表中。

語法

List=list1.append()

示例

#create a list
l1 = []

# create a dictionary
city = {
   2024: 'Delhi',
   2021: 'Lucknow',
   7060: 'Hyderabad',
   1034: 'Gurgaon',
   7062: 'Kanpur'
}

# append this dictionary to the empty list
l1.append(city)

# display the list
l1

輸出

[
 {2024: 'Delhi',

  2021: 'Lucknow',
  7060: 'Hyderabad',
  1034: 'Gurgaon',
  7062: 'Kanpur'

結論

在本文中,我們學習了列表、字典及其語法,以及在python中將字典新增到列表的方法。我們學習了三種方法:一種是使用append函式,一種是使用append函式和copy函式,另一種是使用append函式和deepcopy函式。

更新於:2023年5月31日

4K+ 瀏覽量

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告