How to use an index to get an item?

0 votes
I have a tuple in python ('A','B','C','D','E'), how do I get which item is under a particular index number?

Example: Say it was given 0, it would return A. Given 2, it would return C. Given 4, it would return E.
Jan 30 in Python by ana1504.k
• 6,630 points
14 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
What you show, ('A','B','C','D','E'), is not a list, it's a tuple. But the indexing method is same for list and tuple. In either case you append the index in square brackets.

So you can write it as:

thetuple = ('A','B','C','D','E')
print thetuple[0]
 

prints A, and so forth.
answered Jan 30 by SDeb
• 10,700 points

Related Questions In Python

0 votes
1 answer

How to get value in Pandas dataframe using index?

You can use the iloc method to do ...READ MORE

answered Apr 8 in Python by Rohit
23 views
+2 votes
2 answers

How to use BeatifulSoup for webscraping?

your programme is fine until you start ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,710 points
20 views
+4 votes
7 answers
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,710 points
50 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
54 views
0 votes
1 answer

How to use a global variable in function?

The global variable can be used in ...READ MORE

answered Sep 27, 2018 in Python by SDeb
• 10,700 points
16 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 10,700 points
30 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.