<p>Ok, after some more searching I found the answer. </p>

<p>You have to use the <strong>cmd.iterate</strong> function to iterate over the default selection ("sele", first parameter). With the second parameter you can push the variables you need into an array for later access. In this case I wanted the residue number so I used the variable 'resv'. A full list of the variables exposed by the iterate function can be found on the <a href="https://www.pymolwiki.org/index.php/Iterate" rel="nofollow noreferrer">pymolwiki</a></p>

<p>Note that you can use a function call as the second parameter and if you need a more complex selection than 'sele' you can use selector.process in the iterate function.</p>

<p>Here is my final working function. Note that I used a set for storing the residue ids, since the iterate function technically iterates over the atoms and so the same residue will likely appear multiple times in your selection.</p>

<pre><code>def show_selection_position_data():
    residue_data = {
            "header": ['variation', 'rmsf']
            , 1: ['high', 0.1]
            , 2: ['high', 0.2]
            , 3: ['low', 0.1]
            , 4: ['high', 0.05]
            , 5: ['middle', 0.2]
            , 6: ['high', 0.1]
            , 7: ['high', 0.1]
        }
    selections = cmd.get_names('selections')

    if "sele" not in selections:
        print("No selection")
        return

    userSelection = "sele"

    stored.residues = set()
    cmd.iterate(selector.process(userSelection), 'stored.residues.add(resv)')

    for residue in stored.residues:
        if residue in residue_data:
            print(residue_data[residue])
        else:
            print("{}: no data".format(residue))

    return
</code></pre>

CC BY-SA 4.0
2019-06-14T15:45:34.693Z
Nice. And thank you for taking the time to post an answer!
terdon
CC BY-SA 4.0
2019-06-14T15:44:59.617Z
2019-06-14T15:47:21.523Z
2019-06-14T15:47:21.523Z
C. Zeil
<p>I can make a selection in Pymol using the resi parameter. Is there a way to get this resi data back out of a selection object? The only thing I found so far is how to get the position, as in the x/y/z coordinates from a selection.</p>

<p>For context, I have a table with a set of data points for each residue in the amino acid sequence of the displayed structure and I want to write a function where I can print the data based on the current selection. So my thought would be something like the following, where the content of the <em>'get_residue_positions_for_selection'</em> function would be what I'm asking about.</p>

<pre><code>def show_selection_position_data():
    residue_data = {
        "header": ['variation', 'rmsf']
        , "1": ['high', 0.1]
        , ...
    }
    selections = cmd.get_names('selection')

    if sele not in selections:
        print("No selection")
        return

    residues = get_residue_positions_for_selection("sele")

    for residue in residue_selections:
        print(residue_data[residue])

    return
</code></pre>

CC BY-SA 4.0
2019-06-14T22:48:38.820Z
This certainly a very interesting question and a cool answer. The advice I was given (to pretty much the same problem) was to use Pymol scripting directly rather than via Python.
M__
<pymol>
Get residue position from Pymol Selection
# Get residue position from Pymol Selection

(Asked by: username_0 on 2019-06-14)

I can make a selection in Pymol using the resi parameter. Is there a way to get this resi data back out of a selection object? The only thing I found so far is how to get the position, as in the x/y/z coordinates from a selection.
For context, I have a table with a set of data points for each residue in the amino acid sequence of the displayed structure and I want to write a function where I can print the data based on the current selection. So my thought would be something like the following, where the content of the 'get_residue_positions_for_selection' function would be what I'm asking about.
def show_selection_position_data():
    residue_data = {
        "header": ['variation', 'rmsf']
        , "1": ['high', 0.1]
        , ...
    }
    selections = cmd.get_names('selection')

    if sele not in selections:
        print("No selection")
        return

    residues = get_residue_positions_for_selection("sele")

    for residue in residue_selections:
        print(residue_data[residue])

    return

**Tags:** pymol
> **Comments:**
> **username_1**: This certainly a very interesting question and a cool answer. The advice I was given (to pretty much the same problem) was to use Pymol scripting directly rather than via Python.  

---

## 1 answers

### Answer by username_0 on 2019-06-14. Score: 3 | Accepted answer

Ok, after some more searching I found the answer. 
You have to use the cmd.iterate function to iterate over the default selection ("sele", first parameter). With the second parameter you can push the variables you need into an array for later access. In this case I wanted the residue number so I used the variable 'resv'. A full list of the variables exposed by the iterate function can be found on the pymolwiki
Note that you can use a function call as the second parameter and if you need a more complex selection than 'sele' you can use selector.process in the iterate function.
Here is my final working function. Note that I used a set for storing the residue ids, since the iterate function technically iterates over the atoms and so the same residue will likely appear multiple times in your selection.
def show_selection_position_data():
    residue_data = {
            "header": ['variation', 'rmsf']
            , 1: ['high', 0.1]
            , 2: ['high', 0.2]
            , 3: ['low', 0.1]
            , 4: ['high', 0.05]
            , 5: ['middle', 0.2]
            , 6: ['high', 0.1]
            , 7: ['high', 0.1]
        }
    selections = cmd.get_names('selections')

    if "sele" not in selections:
        print("No selection")
        return

    userSelection = "sele"

    stored.residues = set()
    cmd.iterate(selector.process(userSelection), 'stored.residues.add(resv)')

    for residue in stored.residues:
        if residue in residue_data:
            print(residue_data[residue])
        else:
            print("{}: no data".format(residue))

    return

> **Comments:**
> **username_2**: Nice. And thank you for taking the time to post an answer!  

---

