I have a list 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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you show,
('A','B','C','D','E'), is not alist, it’s atuple(the round parentheses instead of square brackets show that). Nevertheless, whether it to index a list or a tuple (for getting one item at an index), in either case you append the index in square brackets.So:
prints
A, and so forth.Tuples (differently from lists) are immutable, so you couldn’t assign to
thetuple[0]etc (as you could assign to an indexing of a list). However you can definitely just access (“get”) the item by indexing in either case.