List In Python
The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.
Python has six built-in types of sequences, but the most common ones are lists and tuples.
There are certain things we can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.
◆ Lists
The list is a most versatile datatype available in Python which can be written as a list of comma-separated values between square brackets. Important thing about a list is that items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets.
Example=>
subjects = ['OS', 'DBMS', 'TCS'];
marks = [70, 75, 50];
Combo = ["OS", "DBMS", "TCS", 70,75,50] ;
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
To Obtain List Value:
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.
Example => Consider above example of subject and Combo list
print subjects[2]
Output:
TCS
print Combo[2:4]
Output:
['TCS',70,75]
Functions Of List:
List data structure of Python provide a large range of inbuilt function.
This inbulit function decrease the complexity and length of the code.
Some of the list function is discussed below:
1 . cmp(list1, list2)
Compares elements of both lists.
2. len(list)
Gives the total length of the list.
3 . max(list)
Returns item from the list with max value.
4. min(list)
Returns item from the list with min value.
5. list(seq):
Converts a tuple into list.
Comments
Post a Comment