-8

I have a tuple of rows and columns and wanted to access the last row and last column from it.

how to do this?

cs95
  • 379,657
  • 97
  • 704
  • 746
DKM
  • 1,761
  • 2
  • 19
  • 34
  • 2
    Can you give some code that you have tried? – user3483203 Jan 07 '18 at 19:05
  • 1
    I suggest that you learn about the `len()` function. – Code-Apprentice Jan 07 '18 at 19:07
  • 1
    @Code-Apprentice I suggest he learns about **indexing** in general, and **indexing** in python. As you can iterate with negative numbers for reverse purpose in python, he could, instead of using `len()` (that would iterate through all the list) use `-1` that would take the first char, in reverse (the last one). (Maybe that using negative numbers go through the `len()` function, but I'm unsure about this) – IMCoins Jan 07 '18 at 19:09
  • @IMCoins Good point. I forgot about this feature of Python. – Code-Apprentice Jan 07 '18 at 19:17
  • Indexing and slicing explained here, on SO: https://stackoverflow.com/a/509295/8881141 – Mr. T Jan 07 '18 at 19:20

1 Answers1

2

If you have one dimensional tuple, let's say t, you can do t[-1] to get the last element.

Or if your tuple has 2 dimensions, you can use t[-1][-1] to get the last row and last column. For example:

>>> my_tuple = [[1, 2, 3], [4, 5, 6]]
>>> my_tuple[-1][-1]
6
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Klein Tang
  • 21
  • 2
  • Welcome to StackOverflow. Always explain your answers clearly and it's always good to explain the result with the help of example. That way you'll get better response to your answer. Take a look at my edit for reference. :) – Moinuddin Quadri Jan 07 '18 at 19:28
  • 1
    Thank you for editing my answer,. I'm new to StackOverflow, so I'm not very good at type a format answer, but I will try to do it next time. – Klein Tang Jan 07 '18 at 19:48