Hướng dẫn can you append a dictionary to a list in python? - bạn có thể nối từ điển vào danh sách trong python không?

Trong bài viết này, chúng tôi sẽ thảo luận về cách nối từ điển vào cấu trúc dữ liệu danh sách trong Python.

Ở đây chúng tôi sẽ thảo luận:

  1. Nối thêm một từ điển vào một danh sách có cùng một khóa và các giá trị khác nhau
  2. Sử dụng phương thức append ()
  3. Sử dụng Phương thức Copy () & nbsp; để liệt kê bằng phương thức append ()
  4. Sử dụng phương thức DeepCopy () để liệt kê bằng phương thức append ()
  5. Sử dụng Numpy.

Phương pháp 1: Tham gia một từ điển vào danh sách có khóa Thesame và các giá trị khác nhau same key and different values

Ở đây chúng ta sẽ nối một từ điển loại số nguyên vào một danh sách trống bằng cách sử dụng vòng lặp với cùng một khóa nhưng các giá trị khác nhau. Chúng tôi sẽ sử dụng hàm sử dụng zip ()

Cú pháp: list = [dict (zip ([key], [x])) cho x trong phạm vi (bắt đầu, dừng)]]list=[dict(zip([key],[x])) for x in range(start,stop)]

trong đó, khóa là khóa và phạm vi () là phạm vi của các giá trị được nối thêm

Ví dụ: Mã Python để nối 100 giá trị từ 1 đến 100 với 1 làm khóa để liệt kêPython code to append 100 values from 1 to 100 with 1 as key to list

Trong ví dụ này, chúng tôi đang lấy các phần tử từ 1 đến 100 làm giá trị và gán các giá trị đó cho khóa 1 và cuối cùng, chúng tôi đang tăng các yếu tố và cuối cùng, chúng tôi đang nối thêm các yếu tố trong danh sách.

Python3

l =

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[{1: 1}, {1: 2}, {1: 3}, {1: 4}, {1: 5}, {1: 6}, {1: 7}, {1: 8}, { 1: 9}, {1: 10}, {1: 11}, {1: 12}, {1: 13}, {1: 14}, {1: 15}, {1: 16}, {1: 17}, {1: 18}, {1: 19}, {1: 20}, {1: 21}, {1: 22}, {1: 23}, {1: 24}, {1: 25} , {1: 26}, {1: 27}, {1: 28}, {1: 29}, {1: 30}, {1: 31}, {1: 32}, {1: 33}, { 1: 34}, {1: 35}, {1: 36}, {1: 37}, {1: 38}, {1: 39}, {1: 40}, {1: 41}, {1: 42}, {1: 43}, {1: 44}, {1: 45}, {1: 46}, {1: 47}, {1: 48}, {1: 49}, {1: 50} , {1: 51}, {1: 52}, {1: 53}, {1: 54}, {1: 55}, {1: 56}, {1: 57}, {1: 58}, {{ 1: 59}, {1: 60}, {1: 61}, {1: 62}, {1: 63}, {1: 64}, {1: 65}, {1: 66}, {1: 67}, {1: 68}, {1: 69}, {1: 70}, {1: 71}, {1: 72}, {1: 73}, {1: 74}, {1: 75} , {1: 76}, {1: 77}, {1: 78}, {1: 79}, {1: 80}, {1: 81}, {1: 82}, {1: 83}, {{ 1: 84}, {1: 85}, {1: 86}, {1: 87}, {1: 88}, {1: 89}, {1: 90}, {1: 91}, {1: 1: 92}, {1: 93}, {1: 94}, {1: 95}, {1: 96}, {1: 97}, {1: 98}, {1: 99}]

Phương pháp 2: Sử dụng phương thức append ()append() method

Ở đây, chúng ta sẽ nối một từ điển vào danh sách bằng phương thức append (), được sử dụng để nối một mục trong danh sách.

Cú pháp: list.append (từ điển)list.append(dictionary)

where,

  • Danh sách là danh sách đầu vào
  • Từ điển là một từ điển đầu vào được thêm vào

Ví dụ 1: Mã Python để nối từ điển vào danh sách trốngPython code to append a dictionary to an empty list

Ở đây chúng tôi đang coi ID sinh viên là chìa khóa và tên là giá trị như một từ điển, vì vậy chúng tôi đang nối thêm từ điển sinh viên danh sách trống bằng phương pháp phụ lục

Python3

l =

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9=
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4__

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
7
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

Các

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Ví dụ 2: Nối từ điển vào danh sách có chứa các yếu tốAppend a dictionary to the list containing elements

Ở đây chúng tôi đang coi ID sinh viên là chìa khóa và tên là giá trị như một từ điển. Vì vậy, chúng tôi đang nối thêm từ điển sinh viên danh sách đã chứa một số yếu tố bằng phương pháp nối tiếp

Python3

l = [

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3l 0l 1l 0l 3l 0l 5l 6

l 7=

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

=4

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
7
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

=4

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
0
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Phương pháp 3: Sử dụng phương thức Sao chép () và EPPEND ()copy() and append() method

Ở đây, chúng ta sẽ nối một từ điển vào danh sách bằng phương thức append (), chúng ta có thể sử dụng copy () để nối vào danh sách

Cú pháp: từ điển.copy ()dictionary.copy()

Ví dụ 1: Mã Python để nối từ điển vào danh sách trống bằng cách sử dụng phương thức Copy ()Python code to append the dictionary to an empty list by using copy() method

Ở đây chúng tôi đang coi ID sinh viên là khóa và tên là giá trị như một từ điển, vì vậy chúng tôi đang nối thêm từ điển sinh viên với phương thức sao chép danh sách trống bằng phương pháp phụ lục

Python3

l =

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9=
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4__

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
7
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
0
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
22

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Các Append dictionary to list those containing elements using copy() method

Ví dụ 2: Nối từ điển vào danh sách có chứa các yếu tố

Python3

l = [

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3l 0l 1l 0l 3l 0l 5l 6

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9=
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4__

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
7
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
0
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
22

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Phương pháp 4: Sử dụng phương thức DeepCopy () và Phương thức EXPEND ()

Bản sao sâu là một quá trình trong đó quá trình sao chép xảy ra đệ quy, ở đây chúng ta sẽ nối từ điển vào danh sách bằng cách sao chép sâu từ điển.

Cú pháp: append (DeepCopy ())append(deepcopy())

Ví dụ 1: Nối từ điển vào danh sách bằng cách sử dụng Sao chép sâu Append the dictionary to list using deep copy

Ở đây chúng tôi đang coi ID sinh viên là khóa và tên là giá trị như một từ điển, vì vậy chúng tôi đang nối thêm từ điển sinh viên với phương thức DeepCopy Danh sách trống & NBSP; sử dụng phương thức nối tiếp

Python3

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
62
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
63
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
64
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
65

l =

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9=
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
73
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
7
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
73
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9l 0
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
73
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
73
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
8l 0
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
0
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
73
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
94

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Ví dụ 2: Bấp đến từ điển vào danh sách có chứa các yếu tố Appending the dictionary to the list that contains elements

Ở đây chúng tôi đang coi ID sinh viên là chìa khóa và tên là giá trị như một từ điển, vì vậy chúng tôi đang thêm từ điển sinh viên với phương pháp DeepCopy

Python

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
62
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
63
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
64
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
65

l = [

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3l 0l 1l 0l 3l 0l 5l 6

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9=
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4__

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
6
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
7
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
3
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1

Phương pháp 4: Sử dụng phương thức DeepCopy () và Phương thức EXPEND ()

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
94

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
5

Output:

[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]

Bản sao sâu là một quá trình trong đó quá trình sao chép xảy ra đệ quy, ở đây chúng ta sẽ nối từ điển vào danh sách bằng cách sao chép sâu từ điển.

Cú pháp: append (DeepCopy ())

Ví dụ 1: Nối từ điển vào danh sách bằng cách sử dụng Sao chép sâunp.append(res_array, {‘key’: “value”}).tolist()

where,

  • Ở đây chúng tôi đang coi ID sinh viên là khóa và tên là giá trị như một từ điển, vì vậy chúng tôi đang nối thêm từ điển sinh viên với phương thức DeepCopy Danh sách trống & NBSP; sử dụng phương thức nối tiếp
  • [1,
    2,
    'hi',
    'welcome',
    {7058: 'sravan kumsr Gottumukkala',
     7059: 'ojaswi',
     7060: 'bobby',
     7061: 'gnanesh',
     7062: 'rohith'}]
    62
    [1,
    2,
    'hi',
    'welcome',
    {7058: 'sravan kumsr Gottumukkala',
     7059: 'ojaswi',
     7060: 'bobby',
     7061: 'gnanesh',
     7062: 'rohith'}]
    63
    [1,
    2,
    'hi',
    'welcome',
    {7058: 'sravan kumsr Gottumukkala',
     7059: 'ojaswi',
     7060: 'bobby',
     7061: 'gnanesh',
     7062: 'rohith'}]
    64
    [1,
    2,
    'hi',
    'welcome',
    {7058: 'sravan kumsr Gottumukkala',
     7059: 'ojaswi',
     7060: 'bobby',
     7061: 'gnanesh',
     7062: 'rohith'}]
    65
  • l =
    [{7058: 'sravan kumsr Gottumukkala',
     7059: 'ojaswi',
     7060: 'bobby',
     7061: 'gnanesh',
     7062: 'rohith'}]
    8

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
9=
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
2
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
73
[1,
2,
'hi',
'welcome',
{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
1
Python code to append a dictionary to list using NumPy method.

Ví dụ 2: Bấp đến từ điển vào danh sách có chứa các yếu tố

Python3

Ở đây chúng tôi đang coi ID sinh viên là chìa khóa và tên là giá trị như một từ điển, vì vậy chúng tôi đang thêm từ điển sinh viên với phương pháp DeepCopy

Python

Các

Phương pháp 5: Sử dụng Numpy

[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
4
[{7058: 'sravan kumsr Gottumukkala',
 7059: 'ojaswi',
 7060: 'bobby',
 7061: 'gnanesh',
 7062: 'rohith'}]
66

Output:

Numpy là viết tắt của Python số được sử dụng để lưu trữ và xử lý các mảng, ở đây chúng ta sẽ sử dụng Numpy để nối từ điển.