> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# List Operations

> Practical examples of working with lists in Dart

## Working with Lists

This guide demonstrates common list operations through a practical example of managing student grades.

## Creating and Initializing Lists

Start by creating a list with initial values:

```dart theme={null}
List<int> notas = [85, 92, 78];
```

## Adding Elements

Add new elements to your list using the `add()` method:

```dart theme={null}
notas.add(95);
print(notas); // [85, 92, 78, 95]
```

## Displaying Lists

### Print Entire List

You can print the entire list at once:

```dart theme={null}
print(notas); // [85, 92, 78, 95]
```

### Print Individual Elements

Use a `for-in` loop to iterate through each element:

```dart theme={null}
for (int nota in notas) {
  print(nota);
}
```

**Output:**

```
85
92
78
95
```

<Info>
  The `for-in` loop is ideal when you need to access each element but don't need the index position.
</Info>

## Calculating with Lists

### Computing the Sum

Iterate through the list to calculate the total:

```dart theme={null}
int suma = 0;
for (int nota in notas) {
  suma += nota;
}
```

### Computing the Average

Divide the sum by the number of elements using the `length` property:

```dart theme={null}
double promedio = suma / notas.length;
print('Promedio: $promedio');
```

<Tip>
  The `length` property automatically tracks the number of elements, so you don't need to count them manually.
</Tip>

## Complete Example

Here's a complete program that demonstrates list operations:

```dart theme={null}
void main() {

  List<int> notas = [85, 92, 78];

  // Agregar una nota
  notas.add(95);

  //Imprimir la lista
  print(notas);
  print('');

  // Mostrar todos los elementos de las lista por medio de iteraciones.
  for (int nota in notas) {
    print(nota);
  }

  // Calcular promedio
  int suma = 0;
  for (int nota in notas) {
    suma += nota;
  }
  double promedio = suma / notas.length;
  print('Promedio: $promedio');
}
```

**Output:**

```
[85, 92, 78, 95]

85
92
78
95
Promedio: 87.5
```

## Common List Operations

| Operation      | Method/Property    | Description                         |
| -------------- | ------------------ | ----------------------------------- |
| Add element    | `add(value)`       | Adds a single element to the end    |
| Get size       | `length`           | Returns the number of elements      |
| Iterate        | `for-in` loop      | Access each element sequentially    |
| Access element | `list[index]`      | Get element at specific position    |
| Calculate      | Loop + accumulator | Sum, average, or other calculations |

## Practical Use Cases

<CardGroup cols={2}>
  <Card title="Grade Management" icon="graduation-cap">
    Store and calculate student grades, averages, and statistics
  </Card>

  <Card title="Data Collection" icon="database">
    Accumulate data points for analysis and reporting
  </Card>

  <Card title="Inventory Tracking" icon="boxes-stacked">
    Track quantities, prices, or stock levels
  </Card>

  <Card title="Score Keeping" icon="trophy">
    Manage game scores, points, or rankings
  </Card>
</CardGroup>

## Best Practices

<Warning>
  When calculating averages, always check that the list is not empty to avoid division by zero:

  ```dart theme={null}
  if (notas.isNotEmpty) {
    double promedio = suma / notas.length;
  }
  ```
</Warning>

<Tip>
  Use descriptive variable names like `suma` (sum) and `promedio` (average) to make your code more readable.
</Tip>

## Next Steps

Now that you understand basic list operations, you can:

* Learn different ways to iterate through lists
* Explore advanced list methods like `map()`, `where()`, and `reduce()`
* Work with lists of custom objects
* Combine multiple operations for complex data processing
