0

For the record, im a python noob So dont expect to mutch of me.

Lets say i have a Tuple:,

Tuple = {1:'A',5:'B'}

how do I return the second item of this tuple?

I Tried googling it, but I couldnt find anything

I tried

Tuple = {1:'A',5:'B'}

return Tuple[1]

I wanted to return 5 But, It returned 'A'. I know why this is, I just want to know how to return the 2nd item.

  • 1
    Does this answer your question? [Extract the nth key in a python dictionary?](https://stackoverflow.com/questions/16977385/extract-the-nth-key-in-a-python-dictionary) – ekhumoro Nov 17 '22 at 13:20
  • `{1:'A',5:'B'}` is a dict, not a tuple. To get the nth key of a dict, do `list(d)[n]` (or in your example `list(Tuple)[1]` -> `5`). – ekhumoro Nov 17 '22 at 13:23

1 Answers1

0

Tuple = {1:'A',5:'B'} this is not a Tuple variable. It is remaining a dictionary variable.

If You want To access this as a tuple, then you need to conver it into tuple . For Example,

Tuple = tuple({1:'A',5:'B'})
return Tuple[1]

Now this will return 2nd element of Tuple.