The least basic data design in Python is the sequence. Each component of a sequence is allotted a number - its place or index. The initial index is zero, the other index is one, and so off.
Python has six reinforced-in types of sequences, however, the most joint ones are lists and tuples, which we would perceive in this tutorial.
There are definite things that users can opt for with entire sequence types. These operations incorporate indexing, slicing, adding, multiplying, and verifying for membership. Additionally, Python has structured functions for discovering the length of a sequence and its largest and smallest elements.
The database is the most versatile datatype accessible in Python that can be written as a list of comma-separated values (items) between square brackets. The essential thing related to a list is that items in a list require not to be of a similar type.
Developing a list is as simple as putting various comma-separated values between square brackets. For instance :
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]
Same to string indices, list indices start at 0, and lists can be sliced, concatenated, and so on.
To approach values in lists, use the square brackets for slicing along with the index or indices to acquire value accessible at that index. For example :
#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1: ", list1 print "list2[1:5]: ", list2[1:5]
When the above code is executed, it supplies the following result :
list1: physics list2[1:5]: [2, 3, 4, 5]
You can update single or multiple elements of lists by delivering the slice on the left-hand side of the deployment operator, and you can add elements in a list with the append() method. For instance :
#!/usr/bin/python list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : " print list list = 2001; print "New value available at index 2 : " print list
Note ? append() method is discussed in subsequent section.
When the above code is executed, it produces the following result :
Value available at index 2 : 1997 New value available at index 2 : 2001
To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. For example :
#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; print list1 del list1; print "After deleting value at index 2 : " print list1
When the above code is executed, it produces the following result
['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]
Note- remove() method is discussed in subsequent section.
Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.
In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.
len([1, 2, 3])
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
['Hi!'] * 4
['Hi!', 'Hi!', 'Hi!', 'Hi!']
3 in [1, 2, 3]
for x in [1, 2, 3]: print x,
1 2 3
Because lists are sequences, indexing, and slicing work the same way for lists as they do for strings.
Assuming the following input :
L = ['spam', 'Spam', 'SPAM!']
Offsets start at zero
Negative: count from the right
Slicing fetches sections
Does python include the following list functions?
Function with Description
Compares elements of both lists.
Gives the total length of the list.
Returns item from the list with max value.
Returns item from the list with min value.
Converts a tuple into list.
Python includes the following list of methods
Methods with Description
Appends object obj to list
Returns count of how many times obj occurs in list
Appends the contents of seq to list
Returns the lowest index in list that obj appears
Inserts object obj into list at offset index
Removes and returns last object or obj from list
Removes object obj from list
Reverses objects of list in place
Sorts objects of list, use compare func if given
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.