Languages
[Edit]
EN

Python - access list items

0 points
Created by:
mkrieger1
726

In this article, we would like to show you how to access list items in Python.

Quick solution:

my_list = ['A', 'B', 'C']

print(my_list[0])   # A
print(my_list[-3])  # A

 

1. Positive indexing

In this example, we use positive indexing to access list items by refering to the specific index number. We start from index 0 that refers to the first item.

my_list = ['A', 'B', 'C']

print(my_list[0])  # A
print(my_list[1])  # B
print(my_list[2])  # C

Output:

A
B
C

2. Negative indexing

In this example, we use negative indexing to access list items by refering to the specific index number. We start from index -1 that refers to the list item.

my_list = ['A', 'B', 'C']

print(my_list[-1])  # C
print(my_list[-2])  # B
print(my_list[-3])  # A

Output:

C
B
A
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - list (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join