[Tutor] How to get value of sublist as return to verify in other file

Alan Gauld alan.gauld at btinternet.com
Tue Jan 6 01:37:21 CET 2015

On 05/01/15 17:38, shweta kaushik wrote:

>>>> list = [[1,2,3,4,5],[2,3,4,4,6],[3,4,5,4,6],[1,4,5,4,8]]
>>>> for sublist in list:
> ...     if sublist[3] == 4:
> ...        print sublist[3]
> ...
> 4
> 4
> 4
> 4
> I want sublist[3] value when matched first time and don't want the value
> for second match.

You need to break out of the for loop.
The command to do that is 'break'
Just add a break on a line after the print

> And also i want to return same sublist value i.e. sublist[3] and want to
> get the value of sublist[3] in another file.

I'm not sure I understand what you mean there.
If you break out of the loop you can still access
your loop variable. But I'm not sure what you mean
by "get the value of sublist[3] in another file."

Do you want to read another file and compare
it to sublist[3]?
Or do you want to put the value of sublist[3]
into another file?

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list