Python List is a data structure that enables to store sequence of elements. The list is a sequence type and it is the simplest data collection in Python. When it is used to store the elements; it also should provide a mechanism to access those elements. We can use the index operator [] to access the elements from the list. And also, a list is a mutable sequence type; that means, it allows modification of the elements which are stored in the list. Lists allow duplicate elements also.
The elements are stored in the list at different positions. The first element position will start from “0” and the last element position is “Number of elements in the list – 1”. These positions we use in index operator [], to access or modify the elements in the List.
Defining a List
Python allows us to define a list using a square bracket (“[]”). Elements in the list are separated by a comma “,”. The list allows to have different types of data elements; that means; integers, strings, or even different types of data structures.
The below statement creates a list with a sequence of elements: observe that string elements are enclosed in double quotations (“). Remember that, a string should be enclosed in a single (‘) or in double-quotes (“).
>>> lst = [1, 2, "Apple", 8.0] >>> lst [1, 2, "Apple", 8.0]
Access or Modify the elements from the List
As I mentioned above, we can access or modify the elements from the list by using the index operator “[]”. And we need to pass the position of the element. For example, to access the list element at the 0th position, we use “[0]”.
Also, we can access the elements from the end of the list. When we access from the end, the index will start from “-1” (because “0” & “-0” are equal); when we access the list from the beginning, the index starts from “0”. For example, to access the last element from the list, we use “[-1]”.
Below statements shows accessing list elements:
>>> lst[0] 1 >>> lst[3] 8.0 >>> lst[-1] 8.0
Observe from the above statements, lst[3] and lst[-1] are referring to the same list element in the “lst” list.
A list is a sequence, we can use it in a Loop to access or modify its’ elements. For example, we use the “for” statement to access its elements. Below is the code for this:
>>> for i in lst: ... print(i) ... 1 2 Apple 8.0 >>>
To modify the element from the list, we use the index operator. Below statements shows how this will work:
>>> lst[3] = 3.0 >>> lst [1, 2, 'Apple', 3.0] >>>
Observe that, the 4th element “8.0” (position at 3) in the list is replaced with the value “3.0”.
Another important thing, you should notice is; lists are bounded. That means if you try to attempt to access an element using an index value; which doesn’t fall within the bounds of the list; you will get the below Error message:
IndexError: list assignment index out of range
Remove the elements from the List
We can use the “del” statement to remove the elements from the list. For example, to remove the element at index “0”; see the below statement:
>>> del lst[0] >>> lst [2, 'Apple', 3.0] >>>
Observe that, from the above statement(s); the first element was removed and displays available elements in the list.
There is more to discuss on lists. We will discuss more on lists and other sequence types in my upcoming Articles.
/Shijit/
2 thoughts on “Python – Lists – A walk through with Examples”