I want to realize pytontex \pyc as newcommand so that I can use it in a generic way.
When I use the newcommand I just get some strange "i" (I use it for enumerating the spec variable) and no output of the list. Using the same command in the begin document environment creates the output like desired. Maybe the environment and the variables might be the problem...
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[runall=true]{pythontex}
\usepackage{etoolbox} % defines lists and their operations
\newcommand{\createresult}{\pyc{result = ("Peter P", "Charlie D")}}
\newcommand{\odbexporta}{\pyc{numsuf= "i"; j = 0}}
\newcommand{\odbexportb}{\pyc{for j in range(0,2): print(r"\let\spec" + numsuf + "\empty"); newstr = result[j]; print(r"\gdef\spec" + numsuf + r"{" + newstr + r"}"); print(r"\listcsgadd{datensatzspec}{spec" + numsuf + r"}"); numsuf = numsuf + "i"; j = j + 1}}
\begin{document}
OUTPUT BY NEWCOMMAND\\
\createresult
\odbexporta
\odbexportb
%\speci
OUTPUT BY PYC IN BEGIN DOCUMENT\\
\pyc{result = ("Peter P", "Charlie D")}
\pyc{numsuf = "i"; j = 0}
\pyc{for j in range(0,2): print(r"\let\spec" + numsuf + "\empty"); newstr = result[j]; print(r"\gdef\spec" + numsuf + r"{" + newstr + r"}"); print(r"\listcsgadd{datensatzspec}{spec" + numsuf + r"}"); numsuf = numsuf + "i"; j = j + 1}
\speci \\
\specii
\end{document}
UPDATE OF THE QUESTION The answer mentioned below was very useful and solved the initial problem. Thank you so far! Now I added another \newcommand mainly based on \dolistcsloop and \csuse. This command takes all tokens of the name speci, specii, ... out of a list and prints them. When I try to combine the both commands (the one mentioned in the initial question) and the one that prints the \dolistcsloop, I get and \endcsname-error. I think this is caused by some faulty expansion of the macros. How can I solve a situation like this:
\newcommand{\alltogehter}{
\commandA
\commandB
}
where \commandA fetches the information and \commandB prints it out? I know that there have already been some questions to this subject, but the answers in the past didn't solve the problem in appropriate manner.