List in Python
List is the data structure which stores
a group of items of the same or different data types. These elements are stored
sequentially starting with index 0. Lists are the mutable data type means we
can change the value of list items.
List is represented by square brackets [
] and the data is written inside these square brackets separated by comma (,).
Indexing in List
For understanding the indexing of a list, let us consider a list L containing some string data as written below.
L = ["a", "b", "c", "d"]
In a list, each element has a unique index which with we can call that element. Note that index numbers starts from 0 and are consecutive integers. So, for the above list L index numbers can be written as
List |
“a” |
“b” |
“c” |
“d” |
Index No |
0 |
1 |
2 |
3 |
The index number of first item i.e. “a” is equal to 0, for “b” is 1 and so on.
This is one type of indexing. Second type is in reverse order starting from the
right most item. In this the index numbers starts from -1 and decreases by 1 as
we move to right by one element.
List |
“a” |
“b” |
“c” |
“d” |
Index No |
-4 |
-3 |
-2 |
-1 |
So, these are the two types of indexing methods for
a list. Now, let us see how to call the required element from its number.
Calling Item from its index number
For calling any item or element of a list from its
index number, the syntax is
Name of List [index number]
The above expression will give you the value of that
element whose index number is written inside the bracket. Let us understand
this with an example.
So, again consider our list L
= [“a”, “b”, “c”, “d”]. If we write
print (L[0])
print(L[0])
Then what you think what would be the
output. So, the output will be the element written at index 0 of list L i.e. “a”.
Similarly, if we write
print(L[2])
the output would be “c” as its index
number is 2.
So, that’s simple is calling an element from the list using its index number. I hope this would be clear to you. Now, let’s move to the List Slicing.
List Slicing in Python
List slicing means calling a small part of list from the whole list. For this we give starting index number and ending index number and we get a small list containing all elements between these index numbers.
Let us understand it more clearly from example:-
Consider a list L with some integer values stored in it as shown.
L = [10, 20, 35, 48, 63]
Now, let's slice it. For slicing the syntax is
L [ starting index : ending index ]
For example :-
print(L[1 : 4])
If you run the above code, you will find the output
[20, 35, 48]
So, you got the list with elements of index 1, 2 and 3 and not of 4. This is because when we write [1 :4], the sliced list contains element whose index starts from 1 till index 4-1 i.e. 3. This is how we can slice a list. Let's check your understanding with some examples:-
Q - Find Output of the following Code
print(L[1 : 3])
print(L[2 : 5])
print(L[0 : 3])
If you are done with the question check the answer below:-
[20, 35]
[35, 48, 63]
[10, 20, 35]
L = [10, 20, 35, 48, 63]
print(L[1 : 4 : 2])
[20, 48]
Thanks for making this topic very easy for me
ReplyDeletePost a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.