Help with this code

jladasky at itu.edu jladasky at itu.edu
Tue Jan 10 15:10:19 EST 2017


On Monday, January 9, 2017 at 12:50:11 PM UTC-8, Joaquin Alzola wrote:
> >> elements. For example, if we have a list_a=["a","b","c","d"] and
> >> list_b=["a","b"] I want to obtain a new list_c containing elements that
> >> match between these lists (a and b here),
> 
> >Perhaps this might work:
> 
> >>>> list(set(list_a).intersection(set(list_b)))
> >['a', 'b']
> 
> >>> list_a={"a","b","c","d"}
> >>> list_b={"a","b"}
> > sorted(list_a & list_b)
> ['a', 'b']

You might want to use some other names for the objects you defined: "list_a" and "list_b" are not lists, they are sets.  Of course it doesn't matter if it's two throw-away lines typed into the interpreter, but in code that you share with someone else (especially a beginner like the OP), it could be confusing.



More information about the Python-list mailing list