# Finite Enumerated Sets#

class sage.sets.finite_enumerated_set.FiniteEnumeratedSet(elements)[source]#

A class for finite enumerated set.

Returns the finite enumerated set with elements in `elements` where `element` is any (finite) iterable object.

The main purpose is to provide a variant of `list` or `tuple`, which is a parent with an interface consistent with `EnumeratedSets` and has unique representation. The list of the elements is expanded in memory.

EXAMPLES:

```sage: S = FiniteEnumeratedSet([1, 2, 3])
sage: S
{1, 2, 3}
sage: S.list()
[1, 2, 3]
sage: S.cardinality()
3
sage: S.random_element()  # random
1
sage: S.first()
1
sage: S.category()
Category of facade finite enumerated sets
sage: TestSuite(S).run()
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet([Integer(1), Integer(2), Integer(3)])
>>> S
{1, 2, 3}
>>> S.list()
[1, 2, 3]
>>> S.cardinality()
3
>>> S.random_element()  # random
1
>>> S.first()
1
>>> S.category()
Category of facade finite enumerated sets
>>> TestSuite(S).run()
```

Note that being an enumerated set, the result depends on the order:

```sage: S1 = FiniteEnumeratedSet((1, 2, 3))
sage: S1
{1, 2, 3}
sage: S1.list()
[1, 2, 3]
sage: S1 == S
True
sage: S2 = FiniteEnumeratedSet((2, 1, 3))
sage: S2 == S
False
```
```>>> from sage.all import *
>>> S1 = FiniteEnumeratedSet((Integer(1), Integer(2), Integer(3)))
>>> S1
{1, 2, 3}
>>> S1.list()
[1, 2, 3]
>>> S1 == S
True
>>> S2 = FiniteEnumeratedSet((Integer(2), Integer(1), Integer(3)))
>>> S2 == S
False
```

As an abuse, repeated entries in `elements` are allowed to model multisets:

```sage: S1 = FiniteEnumeratedSet((1, 2, 1, 2, 2, 3))
sage: S1
{1, 2, 1, 2, 2, 3}
```
```>>> from sage.all import *
>>> S1 = FiniteEnumeratedSet((Integer(1), Integer(2), Integer(1), Integer(2), Integer(2), Integer(3)))
>>> S1
{1, 2, 1, 2, 2, 3}
```

Finally, the elements are not aware of their parent:

```sage: S.first().parent()
Integer Ring
```
```>>> from sage.all import *
>>> S.first().parent()
Integer Ring
```
an_element()[source]#
cardinality()[source]#
first()[source]#

Return the first element of the enumeration or raise an EmptySetError if the set is empty.

EXAMPLES:

```sage: S = FiniteEnumeratedSet('abc')
sage: S.first()
'a'
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet('abc')
>>> S.first()
'a'
```
index(x)[source]#

Returns the index of `x` in this finite enumerated set.

EXAMPLES:

```sage: S = FiniteEnumeratedSet(['a','b','c'])
sage: S.index('b')
1
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet(['a','b','c'])
>>> S.index('b')
1
```
is_parent_of(x)[source]#
last()[source]#

Returns the last element of the iteration or raise an EmptySetError if the set is empty.

EXAMPLES:

```sage: S = FiniteEnumeratedSet([0,'a',1.23, 'd'])
sage: S.last()
'd'
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet([Integer(0),'a',RealNumber('1.23'), 'd'])
>>> S.last()
'd'
```
list()[source]#
random_element()[source]#

Return a random element.

EXAMPLES:

```sage: S = FiniteEnumeratedSet('abc')
sage: S.random_element()   # random
'b'
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet('abc')
>>> S.random_element()   # random
'b'
```
rank(x)[source]#

Returns the index of `x` in this finite enumerated set.

EXAMPLES:

```sage: S = FiniteEnumeratedSet(['a','b','c'])
sage: S.index('b')
1
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet(['a','b','c'])
>>> S.index('b')
1
```
unrank(i)[source]#

Return the element at position `i`.

EXAMPLES:

```sage: S = FiniteEnumeratedSet([1,'a',-51])
sage: S[0], S[1], S[2]
(1, 'a', -51)
sage: S[3]
Traceback (most recent call last):
...
IndexError: tuple index out of range
sage: S[-1], S[-2], S[-3]
(-51, 'a', 1)
sage: S[-4]
Traceback (most recent call last):
...
IndexError: list index out of range
```
```>>> from sage.all import *
>>> S = FiniteEnumeratedSet([Integer(1),'a',-Integer(51)])
>>> S[Integer(0)], S[Integer(1)], S[Integer(2)]
(1, 'a', -51)
>>> S[Integer(3)]
Traceback (most recent call last):
...
IndexError: tuple index out of range
>>> S[-Integer(1)], S[-Integer(2)], S[-Integer(3)]
(-51, 'a', 1)
>>> S[-Integer(4)]
Traceback (most recent call last):
...
IndexError: list index out of range
```