How to access the low digits of a list

Joel Goldstick joel.goldstick at gmail.com
Tue Jun 2 08:32:00 EDT 2015


On Tue, Jun 2, 2015 at 8:23 AM, fl <rxjwg98 at gmail.com> wrote:
> Hi,
>
> I have a list:
>
>
>
>
>
>
>>>> lines
> ['12', '42', '49', '156', '225', '36', '49', '164', '11181', '3100']
>
>
>
> I want to access the last two digits. That is:
>
> ['12', '42', '49', '56', '25', '36', '49', '64', '81', '00']
>
>
> When I try to use lines[3][0] is '1'
> lines[3][1] is '5'
> lines[3][2] is '6'
>
>
> I don't know whether there is a way to know the length of lines[3]. Then, I
> can use a -1 step to get the last two digits. Or, you may have much better
> ways to do that. Python is really too versatile I feel.
>
> Thanks,
> --
> https://mail.python.org/mailman/listinfo/python-list

You should read about slices

lines[3][-2:]  will give the last two characters in the 3rd group
(starting from 0)

The notation means to start with 2 from the end, and proceed to the end


-- 
Joel Goldstick
http://joelgoldstick.com



More information about the Python-list mailing list