Accessing a List Item
Perhaps, you don’t want the users to see certain items in a list; you can
decide to access only the items you want. To have access to a particular
element in a list, you have to write the list name then followed by the index
of the item, which is enclosed in squared brackets. For instance, we can pull
the third item in our list of items in the kitchen.
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,”
“knife,” “bowls”]
print(kitchen_item [3])
The output will be as follow:
dishwasher
Index Positions
The first index position in Python is not 1 but 0. If you observe in the
example above, you will see that when indexed the third item, we started
counting from 0. You can see that pot (0), kettle (1), fridge (2), while
dishwasher (3). Peradventure, you run a Python program and get a different
result; you have to check if you are making a number error.
Always remember that the second element in any list starts with an index of
1. A simple way of knowing what the particular index number will be is to
substrate one from the item you want to index. Does that sound simple?
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,”
“knife,” “bowls”]
print(kitchen_item)
For instance, we want to access only the item “microwave. To do that, you
have to count the number of items to get to “microwave” and subtract one.
from the code, if you count first, you will discover that “microwave” is the
5
th
element. However, to index it to print only “microwave,” you have to
substrate one from the element. Your code will be like this:
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,”
“knife,” “bowls”]
print(kitchen_item [4])
Let us use another example to index the third and sixth items.
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,”
“knife,” “bowls”]
print(kitchen_item [3])
print(kitchen_item [6])
The output will be as follows:
dishwasher
bowls
Assuming you only want to access the last element, Python has a unique
way of doing that. You can do this by using an index of -1. With this,
Python returns the last item.
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,”
“knife,” “bowls”]
print(kitchen_item [-1])
In this example, the code will return “bowls.” It is easy to remember this
syntax. You do not have to know the particular length of the list. This also
applies to other negative index values. It is like going backward. You can
use the negative index to access the second or third items in a list. For
instance,
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,”
“knife,” “bowls”]
print(kitchen_item [-2])
print(kitchen_item [-3])
The output will be as follows
knife
microwave
Exercise to Try
Write a Python program, which contains a list of all your special
friends. Then print their names by accessing each element in
your list.
From the list, you created above, print a greeting message to
each name you access in the list.
Think of any means of transportation you can remember and
write a list of items used within them. Then make a statement on
some of these elements in the list and print it.
Do'stlaringiz bilan baham: |