The issue with your code is that you try to run a sed command that ooks like
2 5 7 10p
This obviously makes no sense to sed, so it complains.
Part of your issue is using a single string to hold many different items (the line numbers). This is better done with an array. Each array element (line number) will then need to have a p inserted after it to make it a separate sed command.
For the numbers 2, 5, 7, and 10, you want a sed script that looks something like
2p; 5p; 7p; 10p;
or
2p;5p;7p;10p;
or
2p
5p
7p
10p
You can create a script like that rather easily from an array in bash:
lines=( 2 5 7 10 )
printf '%sp\n' "${lines[@]:-"q;"}" | sed -n -f /dev/stdin longText.txt
This uses printf to create a sed script from the list of numbers, and feeds it to sed which reads it from standard input with -f /dev/stdin, and applies it to you file.
If the list is empty, the printf will emit a sed expression that does not do anything and then terminates sed.
If you need to use the numbers in a string, then you could convert that string into a valid sed script and apply it to your file like so:
sed 's/[[:digit:]]\{1,\}/&p;/g' <<<"$list" | sed -n -f /dev/stdin longText.txt
Given the string 2 5 7 10, this would generate the sed script 2p; 5p; 7p; 10p; and then apply that to your file in a similar manner as above.
An empty list would not cause sed to output anything.