Lists and List Slicing in Python

 


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]

I hope this part of List slicing would be clear to you.

Now, let's move in more depth of slicing. So, when we are slicing from some initial index to some other let us say [1:4] then we are getting items at index 1,2 and 3 in are slicing list. Means that we get items of consecutive index numbers. 
But what if you want to get only that items which are at 1 and 3 means with difference in index number 2.

For this we introduce a new parameter called "Step". By default, step is equal to 1 and thus we get items at consecutive index numbers. To change that we just have to write another colon and then the value of step. So, let's see the syntax for this

L [ starting index : ending index : Step ]

That's simple, now let me explain you this with some example. 
Consider the same list L
L = [10, 20, 35, 48, 63]
If we write that following code:-
print(L[1 : 4 : 2])
Then the output would be 
[20, 48]
This is because we gave index from 1 to 4 and a step of 2. So, index number 1 will be taken then 2 will be ignored and then 3 will be taken.

By this we have completed, the list slicing from very basic and covered all the topics under list slicing. In case of any doubt, you can ask me below in comments. I will try to solve your query. If you like the content, share it. And if you want to see video tutorials for List then, visit to my YouTube channel from the link in footer.

Enjoy the Coding!! 

Your appreciations and suggestions are welcomed in comments always.

Want to learn python from basic to advance level - Check Ultimate Python Course
Free Course with detailed notes and video tutorials 


1 Comments

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

Previous Post Next Post