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,910 points
380 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,300 points

Related Questions In Python

0 votes
1 answer

How to find the index of an item in a list?

One thing that is really helpful in ...READ MORE

answered Dec 3, 2020 in Python by Gitika
• 65,910 points
1,165 views
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
7,516 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
• 33,030 points
745 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 24, 2020 in Python by Gitika
• 65,910 points
1,005 views
0 votes
2 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
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,300 points
702 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,300 points
692 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP