この記事では、Python で辞書のリストを作成する方法について説明します。
Input: d = [{7058: 'sravan', 7059: 'jyothika', 7072: 'harsha', 7075: 'deepika'}] print(type(d)) Output: Explanation: The type of input is list data-type having dictionary init.>
Pythonの辞書一覧
この記事で取り上げるトピックは次のとおりです。
- のリストを作成する 辞書 Pythonで
- からの辞書要素へのアクセス Python リスト 辞書の
- Python で複数の辞書のリストを作成する
- 辞書要素へのアクセス 複数の辞書の Python リストから
- 辞書を追加する Python辞書リストへ
- 辞書を更新する Python辞書リストへ
辞書のリストは、辞書が要素として存在することを意味します。 パイソン リスト。
例:
[ {1: 'Geeks', 2: 'techcodeview.com'} ]>
Pythonで辞書のリストを作成する
あ Python リスト の辞書は次の構文で作成できます。
構文:
[ {‘キー’:要素 1, ‘キー’:要素 2, ……, ‘キー’:要素 n} ]
例: この例では、リストを作成し、それに辞書を渡します。この場合、渡される辞書は 1 つだけなので、リストの長さは 1 になります。
Python3
たんぱく質脂肪です
# create a list of dictionary with student> # id as key and name as value> data> => [{> 7058> :> 'sravan'> ,> 7059> :> 'jyothika'> ,> > 7072> :> 'harsha'> ,> 7075> :> 'deepika'> }]> # display data> print> (data)> print> (> len> (data))> print> (> type> (data))> |
>
>
出力:
[{7058: 'sravan', 7059: 'jyothika', 7072: 'harsha', 7075: 'deepika'}] 1>
Python 辞書リストからの辞書要素へのアクセス
の要素にアクセスできます。 Python辞書 インデックスを使用します。インデックスはディクショナリのインデックス、キーはディクショナリのキーと値です。
構文:
データ[インデックス][キー]
例: ここでは、インデックス値を介して辞書の値を取得します。
Python3
# create a list of dictionary with student> # id as key and name as value> data> => [{> 7058> :> 'sravan'> ,> 7059> :> 'jyothika'> ,> > 7072> :> 'harsha'> ,> 7075> :> 'deepika'> }]> # display data of key 7058> print> (data[> 0> ][> 7058> ])> # display data of key 7059> print> (data[> 0> ][> 7059> ])> # display data of key 7072> print> (data[> 0> ][> 7072> ])> # display data of key 7075> print> (data[> 0> ][> 7075> ])> |
>
>
出力:
sravan jyothika harsha deepika>
Python で複数の辞書のリストを作成する
これは、複数の辞書が一度にリストに渡されることを除いて、上記のアプローチに似ています。辞書のリストは、Python で次のように作成できます。
構文:
[ {key1: 要素 1, key2: 要素 2},
{キー1:要素1、キー2:要素2} ]
例: このプログラムでは、リストの要素が辞書となる長さ 3 のリストを Python で作成します。
Python3
# create a list of dictionaries with> # student id as key and name as value> data> => [{> 7058> :> 'sravan'> ,> 7059> :> 'jyothika'> ,> > 7072> :> 'harsha'> ,> 7075> :> 'deepika'> },> > > {> 7051> :> 'fathima'> ,> 7089> :> 'mounika'> ,> > 7012> :> 'thanmai'> ,> 7115> :> 'vasavi'> },> > > {> 9001> :> 'ojaswi'> ,> 1289> :> 'daksha'> ,> > 7045> :> 'parvathi'> ,> 9815> :> 'bhavani'> }]> print> (data)> |
>
>
出力:
[{7058: 'sravan', 7059: 'jyothika', 7072: 'harsha', 7075: 'deepika'}, {7051: 'fathima', 7089: 'mounika', 7012: 'thanmai', 7115: 'vasavi'}, {9001: 'ojaswi', 1289: 'daksha', 7045: 'parvathi', 9815: 'bhavani'}]>
複数の辞書の Python リストから辞書要素にアクセスする
単一の Python 辞書のリストと同様に、インデックスを使用してすべての要素にアクセスできます。
例: この例では、Python の辞書のインデックスとキー値に基づいて特定の要素にアクセスします。
Python3
# create a list of dictionaries with> # student id as key and name as value> data> => [{> 7058> :> 'sravan'> ,> 7059> :> 'jyothika'> ,> > 7072> :> 'harsha'> ,> 7075> :> 'deepika'> },> > > {> 7051> :> 'fathima'> ,> 7089> :> 'mounika'> ,> > 7012> :> 'thanmai'> ,> 7115> :> 'vasavi'> },> > > {> 9001> :> 'ojaswi'> ,> 1289> :> 'daksha'> ,> > 7045> :> 'parvathi'> ,> 9815> :> 'bhavani'> }]> # access third dictionary with key 9001> print> (data[> 2> ][> 9001> ])> # access second dictionary with key 7012> print> (data[> 1> ][> 7012> ])> # access second dictionary with key 7115> print> (data[> 1> ][> 7115> ])> |
>
>
出力:
ojaswi thanmai vasavi>
Python 辞書リストに辞書を追加する
Python を使用して、辞書のリストに新しい辞書を追加できます。 append() メソッド 。
例: この例では、単一の辞書要素のリストがあります。 append() メソッドを使用して、このリストに別の辞書を追加します。
Python3
# create a list of a dictionary> # with single dictionary element> data> => [ {> 1> :> 'Geeks'> ,> 2> :> 'techcodeview.com'> } ]> print> (data)> # create a new dictionary to be appended> new_data> => {> 1> :> 'Python'> ,> 2> :> 'Programming'> }> # appending the new dictionary to> # the original list of dictionary> data.append(new_data)> print> (data)> |
>
>
出力:
[{1: 'Geeks', 2: 'techcodeview.com'}] [{1: 'Geeks', 2: 'techcodeview.com'}, {1: 'Python', 2: 'Programming'}]>
辞書を Python 辞書リストに更新する
辞書のリスト内の Python 辞書の値を更新することもできます。
例: この例では、さまざまな方法で辞書の既存のリストを更新します。まず、新しい値を追加してリストの辞書を更新します。 2 番目に、ディクショナリの既存の値を更新し、3 番目に、ディクショナリのキーと値の要素を削除します。 キーワードの 。
Python3
# create a list of a dictionaries> data> => [{> 1> :> 'Geeks'> ,> 2> :> 'techcodeview.com'> }, {> 1> :> 'Python'> ,> 2> :> 'Programming'> }]> print> (data)> # update the dictionary value> data[> 0> ][> 3> ]> => 'World'> data[> 0> ][> 2> ]> => 'Hello'> del> data[> 1> ][> 2> ]> print> (data)> |
>
>
出力:
[{1: 'Geeks', 2: 'techcodeview.com'}, {1: 'Python', 2: 'Programming'}] [{1: 'Geeks', 2: 'Hello', 3: 'World'}, {1: 'Python'}]>