# Idiom #12 Check if list contains a value

Check if list contains a value x.
list is an iterable finite container.

`x in list`
`with Ada.Containers.Vectors;`
`Result := Items.Contains (X);`
`#include <stdbool.h>`
```bool contains(int x, int* list, size_t list_len) {
for (int i=0 ; i<list_len ; i++)
if (list[i] == x)
return true;
return false;
}```
`(some (partial = x) list)`
`#include <vector>`
```bool Contains(vector<T> list, T x)
{
for (vector<T>::iterator it = list.begin() ; it != list.end() ; ++it)
{
if (*it == x)
return true;
}
return false;
}```
```#include <vector>
#include <algorithm>```
```bool Contains(const std::vector<int> &list, int x)
{
return std::find(list.begin(), list.end(), x) != list.end();
}```
`System.Collections.Generic`
`list.Contains(item);`
`import std.algorithm.searching;`
`bool here = canFind(items, x);`
`list.contains(x);`
`Enum.member?(list, x)`
`lists:member(X, List).`
```member(_, []) -> false;
member(Value, [H|T]) ->
case H of
Value -> true;
_ -> member(T)
end.```
```member(_, []) -> false;
member(Value, [H|_]) where Value =:= H -> true;
member(Value, [_|T]) -> member(Value, T).```
```func Contains(list []T, x T) bool {
for _, item := range list {
if item == x {
return true
}
}
return false
}```
`elem x list`
`list.includes(x)`
`list.indexOf(x) !== -1`
```boolean <T> contains(T[] list, T x){
if( x==null){
for(T y:list)
if( y==null )
return true;
}else{
for(T y:list)
if( x.equals(y) )
return true;
}
return false;
}```
```boolean contains(int[] list, int x){
for(int y:list)
if( y==x )
return true;
return false;
}```
`import java.util.List;`
`list.contains(x)`
```function contains(list, x)
for _, v in pairs(list) do
if v == x then return true end
end
return false
end```
`in_array (\$x, \$list, true);`
`uses classes;`
`result := list.IndexOf(x) <> -1;`
```result := false;
for e in list do
if e=x then
begin
result := true;
break;
end
```
```# To check an array for a value:
print "Found 'foo'\n" if grep { \$_ eq 'foo' } @a_list;

# To check a map for a value:
print "Found 'bar'\n" if exists \$map{bar};```
`list.include?( x )`
`list.contains(&x);`
`list.iter().any(|v| v == &x)`
`(&list).into_iter().any(|v| v == &x)`
```val items: Seq[String] = List("a", "b", "c")
items.contains("a")
```
```(define (contains list x)
(cond
((null? list) #f)
((eq? (car list) x) #t)
(else (contains (cdr list) x))))```

#### Idiom created by

programming-idioms.org