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, 2019 in Python by ana1504.k
• 7,890 points
67 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, 2019 by SDeb
• 13,250 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, 2019 in Python by Rohit
784 views
0 votes
1 answer

How to have values from a List1 into Columns Names and List 2 Values as Index for an Empty DataFrame

Try like this, it will give you ...READ MORE

answered Jul 10, 2019 in Python by Cherukuri
• 32,490 points
202 views
0 votes
1 answer

How to insert an object at a given index in Python?

Hi, @Roshni, I will explain to you with ...READ MORE

answered Jun 23 in Python by Gitika
• 36,930 points
44 views
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,352 views
0 votes
0 answers
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,250 points
110 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,250 points
179 views