Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Python List
In 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. 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 Declaration 1. # a simple list 2. list1 = [1, 2, "Python", "Program", 15.9] 3. list2 = ["Amy", "Ryan", "Henry", "Emma"] 4. 5. # printing the list 6. print(list1) 7. print(list2) 8. 9. # printing the type of list 10. print(type(list1)) 11. print(type(list2)) Output: [1, 2, 'Python', 'Program', 15.9] ['Amy', 'Ryan', 'Henry', 'Emma'] < class ' list ' > < class ' list ' > Characteristics of Lists The characteristics of the List are as follows: o The lists are in order. o The list element can be accessed via the index. o The mutable type of List is o The rundowns are changeable sorts. o The number of various elements can be stored in a list.
1. # list example in detail
2. emp = [ "John", 102, "USA"] 3. Dep1 = [ "CS",10] 4. Dep2 = [ "IT",11] 5. HOD_CS = [ 10,"Mr. Holding"] 6. HOD_IT = [11, "Mr. Bewon"] 7. print("printing employee data ...") 8. print(" Name : %s, ID: %d, Country: %s" % (emp[0], emp[1], emp[2])) 9. print("printing departments ...") 10. print("Department 1:\nName: %s, ID: %d\n Department 2:\n Name: %s, ID: %s"%( Dep1 [0], Dep2[1], Dep2[0], Dep2[1])) 11. print("HOD Details ....") 12. print("CS HOD Name: %s, Id: %d" % (HOD_CS[1], HOD_CS[0])) 13. print("IT HOD Name: %s, Id: %d" % (HOD_IT[1], HOD_IT[0])) 14. print(type(emp), type(Dep1), type(Dep2), ty pe(HOD_CS), type(HOD_IT)) 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 Splitting The 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. 1. list_varible(start:stop:step) o The beginning indicates the beginning record position of the rundown. o The stop signifies the last record position of the rundown. o Within a start, the step is used to skip the nth element: stop. 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 1. list = [1,2,3,4,5,6,7] 2. print(list[0]) 3. print(list[1]) 4. print(list[2]) 5. print(list[3]) 6. # Slicing the elements 7. print(list[0:6]) 8. # By default, the index value is 0 so its starts from the 0th element and go for index -1. 9. print(list[:]) 10. print(list[2:5]) 11. print(list[1:6:2]) 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 1. # negative indexing example 2. list = [1,2,3,4,5] 3. print(list[-1]) 4. print(list[-3:]) 5. print(list[:-1]) 6. print(list[-3:-1]) 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.