Q:

Highlighting items in a list

I'm trying to create a macro where the optional item emboldens an item in the list. I can process the list using the expl3/xparse package and can compare the two strings but I'm having trouble doing both simultaneously.
Can one return compare as an encapsulated macro which accepts one argument or can one extend ProcessList to handle two arguments ? I have read other posts on here but can't seem to find anything that matches this use case.
\documentclass{article}
\ExplSyntaxOn
\newcommand{\compare}[2]{\ifthenelse{\equal{#1}{#2}}{\textbf{#1}}{\emph{#2}}}
\NewDocumentCommand\within{O{} >{\SplitList{,}}m}{%
 \def\compari{\compare{#1}{##2}}
 \ProcessList{#2}{\compari}%
}
\ExplSyntaxOff

\begin{document}
\compare{apples}{bananas}
\compare{apples}{apples}
\within{apples}{apples,bananas}
\end{document}

Expected output
bananas
apples
apples, bananas

A:

The following solution uses a \@for loop, which expects a comma separated list. There is still a bug, for this code breaks down if the list contains spaces.
\documentclass{article}

\makeatletter
\newcommand*{\within}[2]{%
\def\@tempa{#1}%
\def\separator{\def\separator{,\space}}% to place the comma in the printed list only after the first iteration
\@for\x:=#2\do{\separator\ifx\@tempa\x\textbf{\x}\else\emph{\x}\fi}%
}
\makeatother

\begin{document}

\within{apples}{apples,bananas,pears,apples,strawberries} % the apples are bold (twice)

\within{pears}{apples,bananas,pears,apples,strawberries} % the pears are bold

\end{document}

