Danh sách Python giống như các mảng có kích thước động, được khai báo bằng các ngôn ngữ khác [vectơ trong C ++ và ArrayList trong Java]. Trong ngôn ngữ đơn giản, một danh sách là một tập hợp các thứ, được đặt trong [] và được phân tách bởi dấu phẩy. & NBSP;are just like dynamically sized arrays, declared in other languages [vector in C++ and ArrayList in Java]. In simple language, a list is a collection of things, enclosed in [ ] and separated by commas.
Danh sách này là một loại dữ liệu trình tự được sử dụng để lưu trữ bộ sưu tập dữ liệu. Tuples và String là các loại loại dữ liệu chuỗi khác.
Ví dụ về danh sách trong Python
Ở đây chúng tôi đang tạo danh sách Python bằng cách sử dụng [].List using [].
Python3
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks7
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']2221
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']7
Output:
["Geeks", "for", "Geeks"]
Danh sách là các thùng chứa đơn giản nhất là một phần không thể thiếu của ngôn ngữ Python. Danh sách không cần phải đồng nhất luôn luôn làm cho nó trở thành công cụ mạnh mẽ nhất trong Python. Một danh sách duy nhất có thể chứa các kiểu dữ liệu như số nguyên, chuỗi, cũng như các đối tượng. Danh sách là có thể thay đổi, và do đó, chúng có thể bị thay đổi ngay cả sau khi tạo ra chúng.
Tạo một danh sách trong Python
Danh sách trong Python có thể được tạo bằng cách đặt chuỗi bên trong dấu ngoặc vuông []. Không giống như các bộ, một danh sách không cần một chức năng tích hợp để tạo danh sách. & NBSP;
Lưu ý: Không giống như các bộ, danh sách có thể chứa các yếu tố có thể thay đổi. & Nbsp; & nbsp;Unlike Sets, the list may contain mutable elements.
Ví dụ 1: Tạo một danh sách trong Python
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Accessing a element from the list Geeks Geeks0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks3
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Is
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing element using negative indexing Geeks For0
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']7
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]3
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks
Sự phức tạp để tạo danh sách
Độ phức tạp về thời gian: O [1]O[1]
Độ phức tạp không gian: O [n]O[n]
Ví dụ 2: & nbsp; tạo một danh sách với nhiều yếu tố khác biệt hoặc trùng lặp
Một danh sách có thể chứa các giá trị trùng lặp với các vị trí riêng biệt và do đó, nhiều giá trị riêng biệt hoặc trùng lặp có thể được truyền dưới dạng một chuỗi tại thời điểm tạo danh sách.
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]8__
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks04
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Is
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks29
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
Sự phức tạp để tạo danh sách
Độ phức tạp về thời gian: O [1]
Độ phức tạp không gian: O [n]
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks46
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]3
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
Đầu ra
Accessing a element from the list Geeks Geeks
Sự phức tạp để tạo danh sách
Python3
Độ phức tạp về thời gian: O [1]
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks71
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]3
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks78
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks78
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]3
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
Đầu ra
Accessing a element from a Multi-Dimensional list For Geeks
Sự phức tạp để tạo danh sách
Độ phức tạp về thời gian: O [1]
Python3
Is
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']08
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']14
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Accessing element using negative indexing Geeks For
Sự phức tạp để tạo danh sách
Độ phức tạp về thời gian: O [1] O[1]
Độ phức tạp không gian: O [n]O[1]
Ví dụ 2: & nbsp; tạo một danh sách với nhiều yếu tố khác biệt hoặc trùng lặp
Một danh sách có thể chứa các giá trị trùng lặp với các vị trí riêng biệt và do đó, nhiều giá trị riêng biệt hoặc trùng lặp có thể được truyền dưới dạng một chuỗi tại thời điểm tạo danh sách.
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]8__
Is
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']31
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Accessing a element from a Multi-Dimensional list For Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks6
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']29
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']43
Lấy đầu vào của một danh sách Python
Chúng ta có thể lấy đầu vào của một danh sách các phần tử là chuỗi, số nguyên, float, v.v. nhưng cái mặc định là một chuỗi.
Ví dụ 1: & nbsp;
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']44
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']46
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']48
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']50
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']52
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']55
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']56
Output:
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']
Ví dụ 2:
Python
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']57
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']59
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']46
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']63
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']64
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']50
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']67
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']69
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']59
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']46
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']74
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']75
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']55
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']79
Output:
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]
Để biết thêm xem điều này.
Thêm các yếu tố vào danh sách Python
Phương pháp 1: Sử dụng phương thức append []
Các phần tử có thể được thêm vào danh sách bằng cách sử dụng hàm append [] tích hợp. Chỉ có thể thêm một phần tử tại một thời điểm vào danh sách bằng cách sử dụng phương thức append [], để thêm nhiều phần tử với phương thức append [], các vòng lặp được sử dụng. Các bộ dữ liệu cũng có thể được thêm vào danh sách với việc sử dụng phương thức phụ trợ vì các bộ dữ liệu là bất biến. Không giống như các bộ, danh sách cũng có thể được thêm vào danh sách hiện có với việc sử dụng phương thức append [].append[] function. Only one element at a time can be added to the list by using the append[] method, for the addition of multiple elements with the append[] method, loops are used. Tuples can also be added to the list with the use of the append method because tuples are immutable. Unlike Sets, Lists can also be added to the existing list with the use of the append[] method.
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Accessing a element from the list Geeks Geeks0
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']85
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']92
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']92
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']92
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks05
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Accessing a element from the list Geeks Geeks11
Accessing a element from the list Geeks Geeks12___
Accessing a element from the list Geeks Geeks20
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks22
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks25
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks32
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks00
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Initial List: [1, 2, 3, 4] List after performing Insert Operation: ['Geeks', 1, 2, 3, 12, 4]8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']64
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks39
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']31
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks21
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks17
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks53
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks56
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]
Sự phức tạp để thêm các phần tử trong một danh sách [phương thức append []]:
Độ phức tạp về thời gian: O [1] O[1]
Độ phức tạp không gian: O [1]pace Complexity: O[1]
Phương pháp 2: Sử dụng phương thức chèn []
Phương thức EPPEND [] chỉ hoạt động để bổ sung các phần tử ở cuối danh sách, để bổ sung các phần tử tại vị trí mong muốn, phương thức chèn [] được sử dụng. Không giống như append [] chỉ lấy một đối số, phương thức chèn [] yêu cầu hai đối số [vị trí, giá trị]. & Nbsp;
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4___
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks75
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Các
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks82
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks17
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks95
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Initial List: [1, 2, 3, 4] List after performing Insert Operation: ['Geeks', 1, 2, 3, 12, 4]
Sự phức tạp để thêm các phần tử trong một danh sách [phương thức append []]:
Độ phức tạp về thời gian: O [1] O[n]
Độ phức tạp không gian: O [1] O[1]
Phương pháp 2: Sử dụng phương thức chèn []
Phương thức EPPEND [] chỉ hoạt động để bổ sung các phần tử ở cuối danh sách, để bổ sung các phần tử tại vị trí mong muốn, phương thức chèn [] được sử dụng. Không giống như append [] chỉ lấy một đối số, phương thức chèn [] yêu cầu hai đối số [vị trí, giá trị]. & Nbsp;extend[], this method is used to add multiple elements at the same time at the end of the list.
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4___append[] and extend[] methods can only add elements at the end.
Python3
Các
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks75
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from a Multi-Dimensional list For Geeks21
Accessing a element from a Multi-Dimensional list For Geeks22
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks17
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks26
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from a Multi-Dimensional list For Geeks30
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks0
Sự phức tạp để thêm các phần tử trong một danh sách [phương thức append []]:
Độ phức tạp về thời gian: O [1] O[n]
Độ phức tạp không gian: O [1] O[1]
Phương pháp 2: Sử dụng phương thức chèn []
Phương thức EPPEND [] chỉ hoạt động để bổ sung các phần tử ở cuối danh sách, để bổ sung các phần tử tại vị trí mong muốn, phương thức chèn [] được sử dụng. Không giống như append [] chỉ lấy một đối số, phương thức chèn [] yêu cầu hai đối số [vị trí, giá trị]. & Nbsp;
Python3
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks9
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4___
Accessing a element from a Multi-Dimensional list For Geeks53
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from a Multi-Dimensional list For Geeks55
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks1
Sự phức tạp để thêm các phần tử trong một danh sách [phương thức append []]:
Độ phức tạp về thời gian: O [1]
Độ phức tạp không gian: O [1]remove[] function but an Error arises if the element doesn’t exist in the list. Remove[] method only removes one element at a time, to remove a range of elements, the iterator is used. The remove[] method removes the specified item.
Phương pháp 2: Sử dụng phương thức chèn []Remove method in List will only remove the first occurrence of the searched element.
Ví dụ 1:
Python3
Is
Accessing a element from a Multi-Dimensional list For Geeks71
Accessing a element from a Multi-Dimensional list For Geeks72
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks22
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks76
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from a Multi-Dimensional list For Geeks80
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing a element from the list Geeks Geeks85
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks755____34
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from a Multi-Dimensional list For Geeks93
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks00
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from a Multi-Dimensional list For Geeks93
Initial List: [1, 2, 3, 4] List after performing Insert Operation: ['Geeks', 1, 2, 3, 12, 4]8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing element using negative indexing Geeks For02
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks2
Ví dụ 2:
Python3
Is
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks755____34
Đầu ra
Accessing a element from the list Geeks Geeks20
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing element using negative indexing Geeks For47
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing element using negative indexing Geeks For50
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks3
Ví dụ 2:
CácO[n]
Accessing a element from the list
Geeks
Geeks
11 Accessing a element from the list
Geeks
Geeks
12___ O[1]
Sự phức tạp để xóa các phần tử trong một danh sách [loại bỏ [] phương thức]:
Độ phức tạp về thời gian: O [n]
Python3
Độ phức tạp không gian: O [1]
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing element using negative indexing Geeks For70
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing element using negative indexing Geeks For73
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing element using negative indexing Geeks For80
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing element using negative indexing Geeks For85
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks4
Ví dụ 2:
Các O[1]/O[n] [O[1] for removing the last element, O[n] for removing the first and middle elements]
Accessing a element from the list
Geeks
Geeks
11 Accessing a element from the list
Geeks
Geeks
12___ O[1]
Sự phức tạp để xóa các phần tử trong một danh sách [loại bỏ [] phương thức]:
Độ phức tạp về thời gian: O [n]
Độ phức tạp không gian: O [1]
Phương pháp 2: Sử dụng phương thức pop []
Hàm pop [] cũng có thể được sử dụng để xóa và trả về một phần tử khỏi danh sách, nhưng theo mặc định, nó chỉ xóa phần tử cuối cùng của danh sách, để xóa một phần tử khỏi một vị trí cụ thể của danh sách, chỉ số của phần tử được truyền như một lập luận cho phương thức pop [].
Is
[:-Index]
Sự phức tạp để xóa các phần tử trong một danh sách [pop [] phương thức]:
[Index:]
Độ phức tạp về thời gian: O [1]/O [N] [O [1] để loại bỏ phần tử cuối cùng, O [N] để loại bỏ các phần tử đầu tiên và giữa]
[::-1]
Cắt một danh sáchTo print elements of List from rear-end, use Negative Indexes.
Chúng ta có thể nhận được chất nền và người phụ sử dụng một lát cắt. Trong danh sách Python, có nhiều cách để in toàn bộ danh sách với tất cả các yếu tố, nhưng để in một loạt các yếu tố cụ thể từ danh sách, chúng tôi sử dụng thao tác lát cắt. & NBSP;
- Hoạt động lát cắt được thực hiện trên các danh sách với việc sử dụng dấu hai chấm [:] & nbsp;
- Để in các yếu tố từ đầu đến một phạm vi sử dụng:
- [: Mục lục]
- Để in các yếu tố từ sử dụng cuối:
- Để in các yếu tố từ một chỉ mục cụ thể cho đến khi sử dụng cuối cùng & nbsp;
- Để in toàn bộ danh sách theo thứ tự ngược lại, sử dụng & nbsp;
Python3
Lưu ý-Để in các yếu tố của danh sách từ phía sau, sử dụng các chỉ mục âm. & NBSP;
Hiểu về việc cắt lát danh sách:
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks75
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
pr [0] truy cập mục đầu tiên, 2.
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']39
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']42
pr [-4] truy cập mục thứ tư từ cuối, 5.
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']51
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']52
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']53
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']42
pr [2:] truy cập [5, 7, 11, 13], một danh sách các mục từ thứ ba đến cuối.
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']63
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']42
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks5
PR [: 4] truy cập [2, 3, 5, 7], một danh sách các mục từ thứ nhất đến thứ tư.
Python3
Lưu ý-Để in các yếu tố của danh sách từ phía sau, sử dụng các chỉ mục âm. & NBSP;
Accessing a element from a Multi-Dimensional list For Geeks71
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']07
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']09
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing element using negative indexing Geeks For94
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing element using negative indexing Geeks For96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Accessing element using negative indexing Geeks For96
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']00
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']1
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']02
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Accessing a element from the list Geeks Geeks75
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Accessing a element from the list Geeks Geeks4
Hiểu về việc cắt lát danh sách:
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]14
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']42
pr [0] truy cập mục đầu tiên, 2.
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]30
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']42
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']29
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']8
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]37
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']14
Initial blank List: [] List after Addition of Three elements: [1, 2, 4] List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3] List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, [5, 6]] List after Addition of a List: [1, 2, 4, 1, 2, 3, [5, 6], ['For', 'Geeks']]4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']5
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Accessing a element from the list Geeks Geeks2
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]43
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter elements: GEEKS FOR GEEKS The list is: ['GEEKS', 'FOR', 'GEEKS']42
Đầu ra
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks6
Danh sách hiểu
Các toàn bộ danh sách Python được sử dụng để tạo danh sách mới từ các vòng lặp khác như bộ dữ liệu, chuỗi, mảng, danh sách, v.v. Danh sách hiểu bao gồm các dấu ngoặc chứa biểu thức, được thực thi cho mỗi phần tử cùng với vòng lặp để lặp lại trên mỗi phần tử. & nbsp; are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.
Syntax:
newlist = [biểu thức [phần tử] cho phần tử trong OldList nếu điều kiện]
Example:
Python3
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]47
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]71
Để hiểu rõ hơn, mã trên tương tự như sau: & nbsp;
Python3
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]47
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Accessing a element from the list Geeks Geeks0
Is
Accessing a element from the list Geeks Geeks20
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]62
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]54
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]64
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Blank List: [] List of numbers: [10, 20, 14] List Items: Geeks Geeks8
Accessing a element from a Multi-Dimensional list For Geeks71
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]94
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]50
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]50
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]9
Accessing a element from the list Geeks Geeks4
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5] List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']6
Enter the size of list : 4 Enter the integer elements: 6 3 9 10 The list is: [6, 3, 9, 10]71
Tham khảo các bài viết dưới đây để có được thông tin chi tiết về hiểu biết danh sách.
- Danh sách Python hiểu và cắt lát
- Danh sách lồng nhau trong Python
- Liệt kê sự hiểu biết và ord [] trong Python
Ví dụ cơ bản trên danh sách Python
- Chương trình Python để trao đổi các yếu tố đầu tiên và cuối cùng trong danh sách
- Chương trình Python để trao đổi hai yếu tố trong danh sách
- Python - các yếu tố hoán đổi trong danh sách chuỗi
- Python | Cách tìm độ dài của danh sách
- Tối đa hai số trong Python
- Tối thiểu hai số trong Python
Để thực hành hoạt động danh sách cơ bản, vui lòng đọc bài viết này - Danh sách chương trình Python
Phương pháp liệt kê
Nối [] | Thêm một phần tử vào cuối danh sách |
Gia hạn[] | Thêm tất cả các yếu tố của danh sách vào danh sách khác |
Chèn[] | Chèn một mục tại chỉ mục được xác định |
Loại bỏ[] | Xóa một mục khỏi danh sách |
Xa lạ[] | Xóa tất cả các mục khỏi danh sách |
Mục lục[] | Trả về chỉ mục của mục phù hợp đầu tiên |
Đếm[] | Trả về số lượng các mục được thông qua như một đối số |
Loại[] | Sắp xếp các mục trong một danh sách theo thứ tự tăng dần |
Đảo ngược[] | Đảo ngược thứ tự các mục trong danh sách |
sao chép [] | Trả về một bản sao của danh sách |
Để biết thêm, hãy tham khảo bài viết này - Phương pháp danh sách Python
Các hoạt động được đề cập ở trên sửa đổi danh sách chính nó.
Các chức năng tích hợp với danh sách
Nối [] | Thêm một phần tử vào cuối danh sách |
Gia hạn[] | Thêm tất cả các yếu tố của danh sách vào danh sách khác |
Chèn[] | Chèn một mục tại chỉ mục được xác định |
Loại bỏ[] | Xóa một mục khỏi danh sách |
Xa lạ[] | Xóa tất cả các mục khỏi danh sách |
Mục lục[] | Trả về chỉ mục của mục phù hợp đầu tiên |
Đếm[] | Trả về số lượng các mục được thông qua như một đối số |
Loại[] | Sắp xếp các mục trong một danh sách theo thứ tự tăng dần |
Đảo ngược[] | Đảo ngược thứ tự các mục trong danh sách |
sao chép [] | Trả về một bản sao của danh sách |
Để biết thêm, hãy tham khảo bài viết này - Phương pháp danh sách Python | Các hoạt động được đề cập ở trên sửa đổi danh sách chính nó. |
Các chức năng tích hợp với danh sách | giảm[] |
Áp dụng một hàm cụ thể được truyền trong đối số của nó cho tất cả các yếu tố danh sách lưu trữ kết quả trung gian và chỉ trả về giá trị tổng cuối cùng | Tổng[] |
Tổng hợp các số trong danh sách | ord [] |
Trả về một số nguyên đại diện cho điểm mã unicode của ký tự unicode đã cho
cmp []
- Hàm này trả về 1 nếu danh sách đầu tiên là "lớn hơn so với danh sách thứ hai
- Max []
- Trả về phần tử tối đa của một danh sách nhất định
- tối thiểu []