Python lists such as those returned by the string’s split method are sequences of objects. Unlike strings, lists are mutable in so far as the elements within them can be replaced or removed, and new elements can be inserted or appended.
Literal lists are delimited by square brackets, and the items within the list separated by commas. Here is a list of three numbers and a list of three strings. We can retrieve elements by using square brackets with a zero-based index, and we can replace elements by assigning to a specific element. See how lists can be heterogeneous with respect to the types of the objects. We now have a list containing a string, an integer, and another string.
It’s often useful to create an empty list, which we can do using empty square brackets. We can modify the list in other ways.Let’s add some floats to the end of the list using the append method.
See also how we’re allowed to use an additional comma after the last element, an important maintainability feature.
Slicing is a form of extended indexing which allows us to refer to portions of a list.To use it, we pass the start and stop indices of a half-open range separated by a colon as the square brackets index argument.
Here we slice three words from the list by passing the start index 1 and the stop index 4. This facility can be combined with negative indexing. For example, to take all the elements except the first and last, slice between 1 and -1. Both the start and stop indices are optional. To slice all elements from the second to the end of the list, supply only 2: as the argument to the index operator.
we can copy one list object to another list object in the following ways:
You must be aware, that all of these techniques perform a shallow copy. That is, they create a new list containing the same object references as the source list, but don’t copy the referred to objects.
As for strings and tuples, lists support repetition using the multiplication operator. It’s simple enough to use. Here we repeat a list containing the integers 21 and 37 four times
To find an element in a list, use the index method passing the object you’re searching for. The elements are compared for equivalence or value equality until the one you’re looking for is found and its index returned. Here we create a list W containing few words , Searching for fox using the index method returns the integer 3, which of course allows us to get ahold of that element. If you search for a value that isn’t present like unicorn, you will receive a ValueError. Another means of searching is to count matching elements using the count method. Here we count occurrences of the word the. If you just want to test for membership, you can use the in operator or for nonmembership using the not in operator.
Elements from a list can be removed using del or remove as shown below.
Before we move on from lists, let’s look at two operations which rearrange the elements in place, reversing and sorting. A list can be reversed in place simply by calling its reverse() method. Similarly, a list can be sorted in place using the sort() method. The sort() method accepts two optional arguments, key and reverse. The latter is self-explanatory, and when set to true gives a descending sort
Hope you have enjoyed the post.Please share your comments
One thought on “List in python”