Access Items from a List in Python (Indexing, Slicing)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Access Items from a List in Python (Indexing, Slicing)

Of course, list’s elements must be accessible, somehow! If you do not get them by key, like you would with dictionary in Python, then how would you get the items of a List in Python?

You can think of a List as a relationship between indexes, and values. This relationship is called a mapping, where each index maps to one of the values. Thus, individual items in a List in Python can be accessed using an index, in square brackets [].

This is exactly analogous to accessing individual characters in a string. The index must be an integer, or it would result in TypeError.

And, List indexing in Python is zero-based, as it is with strings. So, the first element of a list, will always be at the index zero. The indexes for the values in a list are illustrated as follow.

For instance: a list having 5 elements will have an index vary from 0 through 4. And you can access individual items in a list using its index – in square brackets.

     my_list = ['red', 'green', 'blue', 'yellow', 'black']
     
     my_list[0]   # red
     my_list[2]   # blue

Nonetheless, If you refer to an index that is not in this List, you will get an Exception. To avoid such exception, Python will raise an “IndexError: list index out of range” error, if you use an index that exceeds the number of items in your List.

     my_list = ['red', 'green', 'blue', 'yellow', 'black']
     my_list[5]  # IndexError: list index out of range



○ Negative List Indexing

Virtually everything about string indexing works similarly for lists. Thus, you can access a list by negative indexing, as well. For example, a negative list index counts from the end of the list. So, the index of -1 refers to the last item, -2 to the second last item, and so on. The negative indexes for the items in a list are illustrated as below.

     my_list = ['red', 'green', 'blue', 'yellow', 'black']
     
     my_list[-1]   # black
     my_list[-2]   # yellow



○ Slicing a List

A segment of a list is called a slice. You can access or extract a range of items in a list, by using the slicing operator (:).

The slice operator my_list [ start : stop ] returns the part of the list from the “start-th” item to the “stop-th” item, which will include the first item but exclude the last one. Please note that a slice of a list, is also a list – and you may want to specify a step size parameter. But, if you do not specify the start parameter, it will start from the beginning of this list! On the other hand, if you did not specify the end parameter, it will take all the items until it reaches the end of the list.

     my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
     
     my_list[0:2]   # ['a', 'b']
     my_list[2:7]   # ['c', 'd', 'e', 'f', 'g']
     my_list[3:-1]  # ['d', 'e', 'f', 'g', 'h']
     
     my_list[2:]   # ['c', 'd', 'e', 'f', 'g', 'h', 'i']
     my_list[:5]   # ['a', 'b', 'c', 'd', 'e']

Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap