Python access element in list

Python — Access List Items

List items are indexed and you can access them by referring to the index number:

Example

Print the second item of the list:

Note: The first item has index 0.

Negative Indexing

Negative indexing means start from the end

-1 refers to the last item, -2 refers to the second last item etc.

Example

Print the last item of the list:

Range of Indexes

You can specify a range of indexes by specifying where to start and where to end the range.

When specifying a range, the return value will be a new list with the specified items.

Example

Return the third, fourth, and fifth item:

Note: The search will start at index 2 (included) and end at index 5 (not included).

Remember that the first item has index 0.

By leaving out the start value, the range will start at the first item:

Example

This example returns the items from the beginning to, but NOT including, «kiwi»:

By leaving out the end value, the range will go on to the end of the list:

Example

This example returns the items from «cherry» to the end:

Range of Negative Indexes

Specify negative indexes if you want to start the search from the end of the list:

Example

This example returns the items from «orange» (-4) to, but NOT including «mango» (-1):

Check if Item Exists

To determine if a specified item is present in a list use the in keyword:

Читайте также:  Loading jdbc driver in java

Example

Check if «apple» is present in the list:

thislist = [«apple», «banana», «cherry»]
if «apple» in thislist:
print(«Yes, ‘apple’ is in the fruits list»)

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

How to access List elements in Python?

This article explains how to access single or multiple elements from a list in Python.

A list is a sequential data structure, and all the elements in the List are indexed. Therefore, we can access any element of the List by its index position. Also, the indexing starts from 0 in the List. Let’s understand by an example,

list_of_names = ['John', 'Mark', 'Jose', 'Shan']

Each element in this List has an index position associated with it i.e.

  • Index position of string ‘John’ is 0
  • Index position of string ‘Mark’ is 1
  • Index position of string ‘Jose’ is 2
  • Index position of string ‘Shan’ is 3

Now, let’s see how we can access an element from this List by its index position.

Frequently Asked:

Access nth element in the List

To access an element from the List by its index position, we must pass it in square brackets. Like to access nth element do like this,

Let’s see how to access 3rd element from a list of strings

Access third element from the list

list_of_names = ['John', 'Mark', 'Jose', 'Shan'] # Get third element from list user = list_of_names[2] print(user)

It returned the 3rd element in the List. As indexing starts from 0, so the index position of the third element is 2 here.

Access nth element from last in List using negative indexing

The List also supports negative indexing. Here, the negative index means index position from the end i.e.

Index position of the last element of List is: -1
Index position of 2nd last element of List is: -2
Index position of 3rd last element of List is: -3
Index position of 4rd last element of List is: -4
…..
Index position of nth last element of List is: -n

Читайте также:  Page Title

We can use this negative indexing to access elements from last. Let’s see some examples,

Access last element of List

list_of_names = ['John', 'Mark', 'Jose', 'Shan'] # Get last element from list user = list_of_names[-1] print(user)

Access second last element of List

list_of_names = ['John', 'Mark', 'Jose', 'Shan'] # Get second last element from list user = list_of_names[-2] print(user)

Access multiple elements from List

Access elements from List using index range

You can select multiple items from a list using index range i.e., start & end index positions. For example,

It returns a new list containing selected elements from the calling list object, i.e., from start index position to end-1 index position.

Let’s see some examples,

Access elements from index position 2 to 7 in a list i.e., element at index position 2, 3, 4, 5 and 6

list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements from index position 2 to 7 nums = list_of_numbers[2:7] print(nums)

Access elements from index position 0 to 2 in a list i.e., element at index position 0 and 1

list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements from index position 0 to 2 nums = list_of_numbers[0: 2] print(nums)

Access elements from List using negative index range

List in Python also supports negative index range. It means you can select multiple items from a list at the end i.e., using negative index range i.e. -start & -end index positions. For example,

It returns a new list containing selected elements from the calling list object i.e., from -start index position to -(end-1) index position. Here -n index position means nth element from last.

Let’s see some examples,

Access elements from index position -5 to -2 in a list i.e., element at index position -5, -4, -3 i.e., third last, fouth last, and fifth the last element from List.

list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements at index position -3, -4, -5 nums = list_of_numbers[-5 : -2] print(nums)

Access elements from index position -3 to -1 in a list, i.e., the elements at index position -3 and -2 are the second last and third last element in the List.

list_of_numbers = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] # Access elements at index position -3 and -2 nums = list_of_numbers[-3 : -1] print(nums)

Today we learned how to access single or multiple elements from a list in Python.

Читайте также:  Css animation all properties

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Оцените статью