Python ListIn Python, the sequence of various data types is stored in a list. A list is a collection of different kinds of values or items. Since Python lists are mutable, we can change their elements after forming. The comma (,) and the square brackets [enclose the List's items] serve as separators. Although six Python data types can hold sequences, the List is the most common and reliable form. A list, a type of sequence data, is used to store the collection of data. Tuples and Strings are two similar data formats for sequences. Lists written in Python are identical to dynamically scaled arrays defined in other languages, such as Array List in Java and Vector in C++. A list is a collection of items separated by commas and denoted by the symbol []. List DeclarationCode Output: [1, 2, 'Python', 'Program', 15.9] ['Amy', 'Ryan', 'Henry', 'Emma'] < class ' list ' > < class ' list ' > Characteristics of ListsThe characteristics of the List are as follows:
Ordered List CheckingCode Output: False The indistinguishable components were remembered for the two records; however, the subsequent rundown changed the file position of the fifth component, which is against the rundowns' planned request. False is returned when the two lists are compared. Code Output: True Records forever protect the component's structure. Because of this, it is an arranged collection of things. Let's take a closer look at the list example. Code Output: printing employee data... Name : John, ID: 102, Country: USA printing departments... Department 1: Name: CS, ID: 11 Department 2: Name: IT, ID: 11 HOD Details .... CS HOD Name: Mr. Holding, Id: 10 IT HOD Name: Mr. Bewon, Id: 11 <class ' list '> <class ' list '> <class ' list '> <class ' list '> <class ' list '> In the preceding illustration, we printed the employee and department-specific details from lists that we had created. To better comprehend the List's concept, look at the code above. List Indexing and SplittingThe indexing procedure is carried out similarly to string processing. The slice operator [] can be used to get to the List's components. The index ranges from 0 to length -1. The 0th index is where the List's first element is stored; the 1st index is where the second element is stored, and so on. We can get the sub-list of the list using the following syntax.
The start parameter is the initial index, the step is the ending index, and the value of the end parameter is the number of elements that are "stepped" through. The default value for the step is one without a specific value. Inside the resultant Sub List, the same with record start would be available, yet the one with the file finish will not. The first element in a list appears to have an index of zero. Consider the following example: Code Output: 1 2 3 4 [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 7] [3, 4, 5] [2, 4, 6] In contrast to other programming languages, Python lets you use negative indexing as well. The negative indices are counted from the right. The index -1 represents the final element on the List's right side, followed by the index -2 for the next member on the left, and so on, until the last element on the left is reached. Let's have a look at the following example where we will use negative indexing to access the elements of the list. Code Output: 5 [3, 4, 5] [1, 2, 3, 4] [3, 4] Negative indexing allows us to obtain an element, as previously mentioned. The rightmost item in the List was returned by the first print statement in the code above. The second print statement returned the sub-list, and so on. Updating List ValuesDue to their mutability and the slice and assignment operator's ability to update their values, lists are Python's most adaptable data structure. Python's append() and insert() methods can also add values to a list. Consider the following example to update the values inside the List. Code Output: [1, 2, 3, 4, 5, 6] [1, 2, 10, 4, 5, 6] [1, 89, 78, 4, 5, 6] [1, 89, 78, 4, 5, 25] The list elements can also be deleted by using the del keyword. Python also provides us the remove() method if we do not know which element is to be deleted from the list. Consider the following example to delete the list elements. Code Output: [1, 2, 3, 4, 5, 6] [1, 2, 10, 4, 5, 6] [1, 89, 78, 4, 5, 6] [1, 89, 78, 4, 5, 25] Python List OperationsThe concatenation (+) and repetition (*) operators work in the same way as they were working with the strings. The different operations of list are
Let's see how the list responds to various operators. 1. RepetitionThe redundancy administrator empowers the rundown components to be rehashed on different occasions. Code Output: [12, 14, 16, 18, 20, 12, 14, 16, 18, 20] 2. ConcatenationIt concatenates the list mentioned on either side of the operator. Code Output: [12, 14, 16, 18, 20, 9, 10, 32, 54, 86] 3. LengthIt is used to get the length of the list Code Output: 9 4. IterationThe for loop is used to iterate over the list elements. Code Output: 12 14 16 39 40 5. MembershipIt returns true if a particular item exists in a particular list otherwise false. Code Output: False False False True True True Iterating a ListA list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows. Code Output: John David James Jonathan Adding Elements to the ListThe append() function in Python can add a new item to the List. In any case, the annex() capability can enhance the finish of the rundown. Consider the accompanying model, where we take the components of the rundown from the client and print the rundown on the control center. Code Output: Enter the number of elements in the list:10 Enter the item:32 Enter the item:56 Enter the item:81 Enter the item:2 Enter the item:34 Enter the item:65 Enter the item:09 Enter the item:66 Enter the item:12 Enter the item:18 printing the list items.. 32 56 81 2 34 65 09 66 12 18 Removing Elements from the ListThe remove() function in Python can remove an element from the List. To comprehend this idea, look at the example that follows. Example - Code Output: printing original list: 0 1 2 3 4 printing the list after the removal of first element... 0 1 3 4 Python List Built-in FunctionsPython provides the following built-in functions, which can be used with the lists.
len( )It is used to calculate the length of the list. Code Output: 6 Max( )It returns the maximum element of the list Code Output: 782 Min( )It returns the minimum element of the list Code Output: 103 Let's have a look at the few list examples. Example: 1- Create a program to eliminate the List's duplicate items. Code Output: [1, 2, 3, 55, 98, 65, 13, 29] Example:2- Compose a program to track down the amount of the component in the rundown. Code Output: The sum is: 67 In [8]: Example: 3- Compose the program to find the rundowns comprise of somewhere around one normal component. Code Output: The common element is: 2 Next TopicPython Tuples |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India