To create an empty list you can use the `list()`

function of R programming language, An empty list is a list without elements inside it. In the R, a list is a versatile and fundamental data structure that allows you to store and organize different data objects, such as vectors, matrices, data frames, and even other lists.

Creating empty lists in R is a fundamental operation often used as a starting point for data manipulation and storage. In this article, I will explain various methods to create an empty list with different characteristics. I’ll cover creating simple empty lists, assigning names to an empty list, specifying the length of an empty list, and creating multiple empty lists. These methods offer flexibility and are essential for efficient data handling in R.

**Key points-**

- An empty list in R is a list structure with zero elements, providing a starting point for building and populating lists during data manipulation and analysis.
- Creating an empty list is typically done using the
`list()`

function with no arguments, resulting in a list with a length of zero. - Empty lists serve as placeholders that can be dynamically populated with elements of different data types, making them flexible for a variety of use cases.
- Checking whether a list is empty can be done using functions like
`length()`

or`is.null()`

, which help in handling conditional logic based on the presence or absence of elements. - Empty lists are commonly used in situations where the goal is to iteratively fill or extend the list during a loop or when building complex data structures where elements will be added incrementally.

## Create an Empty List using list()

One of the most commonly used methods to create an empty list in R programming is by using the `list()`

function without passing any elements inside the parentheses. You can get an empty list length and type by using the `length()`

and `class()`

functions respectively.

```
# Create an empty list of 0 length using list()
empty_list <- list()
empty_list
# Type of the object
class(empty_list)
# Length of the empty list
length(empty_list)
```

Yields below output.

When you run this code, you’ll see that `empty_list`

is an empty list (`list()`

), its class is `"list"`

, and its length is `0`

. This is a common way to initialize an empty list in R.

## Create an Empty List with a Name

If you want to create an empty list with names in R, you can follow these steps. First, you can assign names to an empty list using a vector. Next, create a new empty list with the same length as the names vector by using the `vector()`

function. Make sure to specify the list type as `"list"`

and `length`

as the length of the names vector.

```
# Create an empty list with names
empty_list.names <- c("a", "b", "c")
empty_list <- vector("list", length(empty_list.names))
print("Empty list")
empty_list
# Names of the empty list
empty_list.names
# Length of the of the empty list
length(empty_list)
```

Yields below output.

The above code has created an empty list along with the specified length.

## Create an Empty List of Specified Length Using vector(mode = “list”)

Another approach is using the `vector()`

function by passing mode and length to create an empty list. You can specify the mode of the vector() function with “list” and length with 3 and pass them into this function, it will return the empty list with a specified length.

```
# Create empty list of specified length
empty_list_vector <- vector(mode = "list", length = 3)
empty_list_vector
# Length of the empty list
length(empty_list_vector)
# Output:
# > empty_list_vector
# [[1]]
# NULL
# [[2]]
# NULL
# [[3]]
# NULL
# > length(empty_list_vector)
# [1] 3
```

The above code returns `empty_list_vector`

with three elements, and each element set to `NULL`

(empty).

## Create an Empty List using Multiple Empty List

You can also create an empty list by combining two empty lists using R vector. Below is an example.

```
# Create empty list using multiple empty lists
empty_list <- c(list(), list())
empty_list
# Type of the object
class(empty_list)
# Length of the empty list
length(empty_list)
# Output:
# > empty_list
# list()
# > class(empty_list)
# [1] "list"
# > length(empty_list)
# [1] 0
```

## Create Multiple Empty Lists in R

Similarly, You can use the `replicate()`

function to create a specified number of empty lists in R. This function replicates the expression a specified number of times.

```
# Create multiple empty lists
empty_list <- replicate(5, list(), simplify = FAlse)
# Type of the object
class(empty_list)
# Length of the empty list
length(empty_list)
# Output:
# > class(empty_list)
# [1] "list"
# > length(empty_list)
# [1] 5
```

So, after executing this code, the variable `empty_list`

will be a list containing 5 empty lists. Each element of the list is an empty list created by the `list()`

function, and there are 5 such elements in the final list.

## Using as.list() with an empty vector

The final approach is to use the `as.list()`

function to create an empty list by converting an empty vector. Let’s pass the integer(0) into using the as.list() function to convert the integer(vector) to an empty list.

```
# Convert empty vector to list
empty_list <- as.list(integer(0))
class(integer(0))
class(empty_list)
# Length of the empty list
length(empty_list)
# Output:
> class(integer(0))
[1] "integer"
> class(empty_list)
[1] "list"
> length(empty_list)
[1] 0
```

## Conclusion

In this article, I have explained multiple techniques to create empty lists in R, each serving different purposes based on your specific needs. Whether you require a basic empty list, an empty list with names, or an empty list with a specified length, the presented methods provide versatility. Understanding these approaches is crucial for efficiently initializing lists before populating them with data. By incorporating these techniques into your R programming toolkit, you enhance your ability to manipulate and organize data effectively.

## Related Articles

- How to Convert Vector to List in R?
- How to Convert List to Vector in R?
- How to Convert List to String?
- R- create DatFrame with column names
- How to Add Elements to a List using Loop in R?