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
• 7,870 points
16 views

1 answer to this question.

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
• 13,160 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
64 views
0 votes
1 answer
+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
31 views
+4 votes
7 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 169 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
• 13,160 points
19 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
• 13,160 points
51 views