---
title: "UpTo"
language: "en"
type: "Symbol"
summary: "UpTo[n] represents up to n objects or positions. If n objects or positions are available, all are used. If fewer are available, only those available are used."
keywords: 
- up to
- at most
- not more than
- with limit
- not beyond
canonical_url: "https://reference.wolfram.com/language/ref/UpTo.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Graphics Shape & Size"
    link: "https://reference.wolfram.com/language/guide/GraphicsShapeAndSize.en.md"
  - 
    title: "Elements of Lists"
    link: "https://reference.wolfram.com/language/guide/ElementsOfLists.en.md"
related_functions: 
  - 
    title: "Take"
    link: "https://reference.wolfram.com/language/ref/Take.en.md"
  - 
    title: "Drop"
    link: "https://reference.wolfram.com/language/ref/Drop.en.md"
  - 
    title: "Partition"
    link: "https://reference.wolfram.com/language/ref/Partition.en.md"
  - 
    title: "RandomSample"
    link: "https://reference.wolfram.com/language/ref/RandomSample.en.md"
  - 
    title: "StringTake"
    link: "https://reference.wolfram.com/language/ref/StringTake.en.md"
  - 
    title: "ImageTake"
    link: "https://reference.wolfram.com/language/ref/ImageTake.en.md"
  - 
    title: "ImageSize"
    link: "https://reference.wolfram.com/language/ref/ImageSize.en.md"
  - 
    title: "Eigenvalues"
    link: "https://reference.wolfram.com/language/ref/Eigenvalues.en.md"
  - 
    title: "Min"
    link: "https://reference.wolfram.com/language/ref/Min.en.md"
---
# UpTo

UpTo[n] represents up to n objects or positions. If n objects or positions are available, all are used. If fewer are available, only those available are used.

## Details

* ``UpTo`` is used in functions such as ``Take``, ``Partition`` and ``Eigenvalues`` that require or allow requesting a specific number of elements to use or return.

## Examples (16)

### Basic Examples (7)

Take up to the number of elements available:

```wl
In[1]:= Take[{a, b, c, d}, UpTo[6]]

Out[1]= {a, b, c, d}

In[2]:= Take[{a, b, c, d}, UpTo[2]]

Out[2]= {a, b}
```

---

Drop up to the number of elements available:

```wl
In[1]:= Drop[{a, b, c, d}, UpTo[6]]

Out[1]= {}
```

---

Take up to the number of elements available, dropping what remains, if anything:

```wl
In[1]:= TakeDrop[{a, b, c, d}, UpTo[6]]

Out[1]= {{a, b, c, d}, {}}
```

---

Partition into blocks that are up to 3 elements long; the last block is shorter:

```wl
In[1]:= Partition[Range[10], UpTo[3]]

Out[1]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10}}
```

---

Give the 10 most common elements, or as many as are available:

```wl
In[1]:= Commonest[RandomInteger[{0, 5}, 15], UpTo[10]]

Out[1]= {5, 4, 0, 1, 2, 3}
```

---

Give the order of the 10 smallest elements, or as many as are available:

```wl
In[1]:= Ordering[RandomInteger[{0, 5}, 5], UpTo[10]]

Out[1]= {2, 5, 1, 3, 4}
```

---

Give the maximum height and width of a graphic:

```wl
In[1]:= Table[Graphics[Circle[], Background -> LightGreen, ImageSize -> UpTo[100], AspectRatio -> n], {n, {0.5, 1, 2}}]

Out[1]= {[image], [image], [image]}
```

### Scope (7)

Take up to 20 characters from a string:

```wl
In[1]:= StringTake["abcdefghijklm", UpTo[20]]

Out[1]= "abcdefghijklm"
```

---

Find the 4 largest eigenvalues in a matrix, or as many as there are if fewer:

```wl
In[1]:= Eigenvalues[Table[N[1 / (i + j + 1)], {i, 3}, {j, 3}], UpTo[4]]

Out[1]= {0.657051, 0.0189263, 0.000212737}
```

---

Create a "ragged" partition where last elements can be shorter:

```wl
In[1]:= Partition[{a, b, c, d, e, f, g}, UpTo[3]]

Out[1]= {{a, b, c}, {d, e, f}, {g}}

In[2]:= Partition[{a, b, c, d, e, f, g}, UpTo[3], 1]

Out[2]= {{a, b, c}, {b, c, d}, {c, d, e}, {d, e, f}, {e, f, g}, {f, g}, {g}}
```

---

Obtain the 6 most common elements, or as many as there are if fewer:

```wl
In[1]:= Commonest[{b, a, c, 2, a, b, 1, 2}, UpTo[6]]

Out[1]= {b, a, c, 2, 1}
```

---

Generate a random sample of up to 6 elements, or as many as there are if fewer:

```wl
In[1]:= RandomSample[Range[5], UpTo[6]]

Out[1]= {4, 5, 3, 2, 1}
```

---

Obtain the first 4 minimal elements, or as many as are available:

```wl
In[1]:= MinimalBy[{{a, 1}, {b, 1}, {b, 3}}, First, UpTo[4]]

Out[1]= {{a, 1}, {b, 1}, {b, 3}}
```

---

Partition an image so that lengths are up to 36 pixels and heights are up to 28 pixels:

```wl
In[1]:= Grid[ImagePartition[[image], {UpTo[36], UpTo[28]}]]

Out[1]=
|         |         |         |         |         |
| ------- | ------- | ------- | ------- | ------- |
| [image] | [image] | [image] | [image] | [image] |
| [image] | [image] | [image] | [image] | [image] |
| [image] | [image] | [image] | [image] | [image] |
| [image] | [image] | [image] | [image] | [image] |
| [image] | [image] | [image] | [image] | [image] |
```

### Properties & Relations (2)

``Cases`` behaves as though it implicitly uses ``UpTo`` :

```wl
In[1]:= Cases[{1, "f", g, "h", "j"}, _ ? StringQ, 1, 4]

Out[1]= {"f", "h", "j"}
```

You can give an explicit ``UpTo`` specification to ``Cases`` :

```wl
In[2]:= Cases[{1, "f", g, "h", "j"}, _ ? StringQ, 1, UpTo[4]]

Out[2]= {"f", "h", "j"}
```

---

``ImageTake`` behaves as though it implicitly uses ``UpTo`` :

```wl
In[1]:= i1 = ImageTake[[image], 200]

Out[1]= [image]
```

You can give an explicit ``UpTo`` specification to ``ImageTake`` :

```wl
In[2]:= i2 = ImageTake[[image], UpTo[200]]

Out[2]= [image]

In[3]:= i1 === i2

Out[3]= True
```

## See Also

* [`Take`](https://reference.wolfram.com/language/ref/Take.en.md)
* [`Drop`](https://reference.wolfram.com/language/ref/Drop.en.md)
* [`Partition`](https://reference.wolfram.com/language/ref/Partition.en.md)
* [`RandomSample`](https://reference.wolfram.com/language/ref/RandomSample.en.md)
* [`StringTake`](https://reference.wolfram.com/language/ref/StringTake.en.md)
* [`ImageTake`](https://reference.wolfram.com/language/ref/ImageTake.en.md)
* [`ImageSize`](https://reference.wolfram.com/language/ref/ImageSize.en.md)
* [`Eigenvalues`](https://reference.wolfram.com/language/ref/Eigenvalues.en.md)
* [`Min`](https://reference.wolfram.com/language/ref/Min.en.md)

## Related Guides

* [Graphics Shape & Size](https://reference.wolfram.com/language/guide/GraphicsShapeAndSize.en.md)
* [Elements of Lists](https://reference.wolfram.com/language/guide/ElementsOfLists.en.md)

## History

* [Introduced in 2015 (10.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn103.en.md)