Pick rows by integer position from a list of records — first collecting target records via a position list, then extracting a field. With pandas, df.iloc accepts a slice or a list of integers and returns a sub-DataFrame in one step.

By hand

Loop over a positions list, appending records[i] to selected. Then loop over selected to collect the target field.

naive.py
Replay: real traced execution (multi-file project)
# trace: ignore records
records = [
    {'name': 'al', 'age': 20},
    {'name': 'bo', 'age': 25},
    {'name': 'cy', 'age': 22},
    {'name': 'di', 'age': 28},
]
positions = [0, 2]
selected = []
for i in positions:
    selected.append(records[i])
names = []
for r in selected:
    names.append(r['name'])
print('RESULT:', names)
  1. {'name': 'al', 'age': 20},

    2records = [3    {'name': 'al', 'age': 20},4    {'name': 'bo', 'age': 25},
  2. {'name': 'bo', 'age': 25},

    3{'name': 'al', 'age': 20},4{'name': 'bo', 'age': 25},5{'name': 'cy', 'age': 22},
  3. {'name': 'cy', 'age': 22},

    4{'name': 'bo', 'age': 25},5{'name': 'cy', 'age': 22},6{'name': 'di', 'age': 28},
  4. {'name': 'di', 'age': 28},

    5    {'name': 'cy', 'age': 22},6    {'name': 'di', 'age': 28},7]
  5. records = [

    1# trace: ignore records2records = [3    {'name': 'al', 'age': 20},
  6. positions ← [0, 2]

    7]8positions = [0, 2]9selected = []
    values this step[0, 2]positions
  7. selected ← []

    8positions = [0, 2]9selected = []10for i in positions:
    values this step[]selected
  8. i ← 0

    9selected = []10for i in positions:11    selected.append(records[i])
    values this step0i
  9. selected ← [{'name': 'al', 'age': 20}]

    10for i in positions:11    selected.append(records[i])12names = []
    values this step[] [{'name': 'al', 'age': 20}]selected
  10. i ← 2

    9selected = []10for i in positions:11    selected.append(records[i])
    values this step0 2i
  11. selected ← [{'name': 'al', 'age': 20}, {'name': 'cy', 'age': 22}]

    10for i in positions:11    selected.append(records[i])12names = []
    values this step[{'name': 'al', 'age': 20}] [{'name': 'al', 'age': 20}, {'name': 'cy', 'age': 22}]selected
  12. for i in positions:

    9selected = []10for i in positions:11    selected.append(records[i])
  13. names ← []

    11    selected.append(records[i])12names = []13for r in selected:
    values this step[]names
  14. r ← {'name': 'al', 'age': 20}

    12names = []13for r in selected:14    names.append(r['name'])
    values this step{'name': 'al', 'age': 20}r
  15. names ← ['al']

    13for r in selected:14    names.append(r['name'])15print('RESULT:', names)
    values this step[] ['al']names
  16. r ← {'name': 'cy', 'age': 22}

    12names = []13for r in selected:14    names.append(r['name'])
    values this step{'name': 'al', 'age': 20} {'name': 'cy', 'age': 22}r
  17. names ← ['al', 'cy']

    13for r in selected:14    names.append(r['name'])15print('RESULT:', names)
    values this step['al'] ['al', 'cy']names
  18. for r in selected:

    12names = []13for r in selected:14    names.append(r['name'])
  19. stdout ← RESULT: ['al', 'cy']

    14    names.append(r['name'])15print('RESULT:', names)
    values this stepRESULT: ['al', 'cy']stdout

With pandas

df.iloc[0:2] selects a contiguous slice by position (end-exclusive). df.iloc[[0, 2]] selects an arbitrary list of positions. Both return a sub-DataFrame preserving the original column structure.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

records = [
    {'name': 'al', 'age': 20},
    {'name': 'bo', 'age': 25},
    {'name': 'cy', 'age': 22},
    {'name': 'di', 'age': 28},
]
df = pd.DataFrame(records)
slice2 = df.iloc[0:2]
by_pos = df.iloc[[0, 2]]
print('slice [0:2] names:', slice2['name'].tolist())
print('index [0,2] names:', by_pos['name'].tolist())
print('index [0,2] shape:', by_pos.shape)
print('RESULT:', by_pos['name'].tolist())
slice [0:2] names: ['al', 'bo']
index [0,2] names: ['al', 'cy']
index [0,2] shape: (2, 2)
RESULT: ['al', 'cy']

Implementation notes

  • iloc is positional: it counts rows from 0 regardless of the index label. Slices are end-exclusive (iloc[0:2] → rows 0 and 1), exactly like Python list slicing.
  • Passing a list (iloc[[0, 2]]) selects non-contiguous rows; passing a slice (iloc[0:2]) selects a contiguous range. The return type is a DataFrame in both cases.
  • iloc also accepts a second argument for column positions: df.iloc[0:2, 0] returns the first column of the first two rows as a Series.
  • Contrast with loc (see select-rows-loc): loc uses index labels, not integer positions, and its slices are end-inclusive.