Questions tagged [empty-list]

An empty list is a container of `list` type that has no content. It can be more generally defined as having a length equal to zero.

An empty list is a container of list type that has no content.

list can cover many types, depending on the language: Python's list, Java's List interface, Caml's List, C++'s std::list, etc. For more details, see .

Most if not all languages implementing a list type also provide a length function, that measures the length of a list, thus accepting the concept of "length". Therefore, the most general definition of "having no content" would be:

Has no content, a list whose length is zero.

Programmatically speaking, a list can be checked empty by many different ways, depending on the language's implementation. Here are some examples of functions returning a truthy value if the input list is empty.

Python

def is_empty(l):
    return l == []

def is_empty(l):
    return not bool(l)

def is_empty(l):
    return len(l) == 0

Java

boolean isEmpty(List<E> list) {
    return list.isEmpty();
}
217 questions
256
votes
7 answers

Collections.emptyList() vs. new instance

In practice, is it better to return an empty list like this: return Collections.emptyList(); Or like this: return new ArrayList(); Or is this completely dependent upon what you're going to do with the returned list?
mre
  • 40,416
  • 33
  • 117
  • 162
82
votes
7 answers

Is there an "Empty List" singleton in C#?

In C# I use LINQ and IEnumerable a good bit. And all is well-and-good (or at least mostly so). However, in many cases I find myself that I need an empty IEnumerable as a default. That is, I would like for (var x in xs) { ... } to work without…
user166390
62
votes
4 answers

Why use null function instead of == [] to check for empty list in Haskell?

I am reading through the "Starting Out" chapter of Learn You a Haskell for Great Good!. It says: null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called…
徐保钰
  • 793
  • 6
  • 15
31
votes
7 answers

How can I make multiple empty lists in python?

How can I make many empty lists without manually typing list1=[] , list2=[], list3=[] Is there a for loop that will make me 'n' number of such empty lists?
Sameer Patel
  • 737
  • 3
  • 12
  • 19
20
votes
9 answers

Collections.emptyList() instead of null check?

If I have a rarely used collection in some class which may be instantiated many times, I may sometimes resort to the following "idiom" in order to save unnecessary object creations: List list = null; void add(Object object) { if (list…
Philip Kamenarsky
  • 2,545
  • 2
  • 19
  • 29
18
votes
4 answers

Checking if a list is empty in java 8

I am new to Java8. I have implemented lists and filters. I have done a null check in my code. I would appreciate some help with how to check if the list is not empty in the same code snippet. If the list is not empty then the code should proceed…
Drools_user
  • 191
  • 1
  • 1
  • 10
17
votes
6 answers
15
votes
1 answer

When does Python create new list objects for empty lists?

The following makes sense to me: >>> [] is [] False Given that lists are mutable, I would expect [] to be a new empty list object every time it appears in an expression. Using this explanation however, the following surprises me: id([]) ==…
Loax
  • 565
  • 3
  • 11
9
votes
2 answers

Selecting rows of pandas DataFrame where column is not empty list

I have a pandas DataFrame where one column contains lists and would like to select the rows where the lists are not empty. Example data: df = pd.DataFrame({'letter': ["a", "b", "c", "d", "e"], …
Chinntimes
  • 135
  • 1
  • 6
8
votes
1 answer

F# Create an empty Array of a pre-defined size

So I'm trying to create an empty Array that is the length of a table row. I know how to get the length of a row, but I haven't got a clue in how to make an array with a pre-defined length. The program i'm making is dynamic so the length of the array…
Mark
  • 1,475
  • 1
  • 7
  • 22
8
votes
2 answers

Generics: Cannot convert from Collections.emptyList() to List

Why public List getList(){ if (isMyListOKReady()) return myList; return Collections.emptyList(); } compiles well, but for public List getList(){ return isMyListReady() ? myList :…
Nic Stray
  • 155
  • 1
  • 9
7
votes
1 answer

haskell implementation of "zip" strange error

I have the following implementation of the zip function in haskell myzip (a:b) (z:g) | b == [] = [] | g == [] = [] | otherwise = (a,z) : myzip b g When I load it into ghci, I get the following error No instance for (Eq b) arising from…
MYV
  • 3,734
  • 5
  • 24
  • 24
6
votes
3 answers

Is there a tensorflow equivalent to np.empty?

Numpy has this helper function, np.empty, which will: Return a new array of given shape and type, without initializing entries. I find it pretty useful when I want to create a tensor using tf.concat since: The number of dimensions of the input…
Andrés Marafioti
  • 628
  • 1
  • 6
  • 17
5
votes
4 answers

'() vs () in Common Lisp

In Common Lisp, it seems like () is a self-evaluating form. That is, it evaluates to itself (or its alias nil). So there would seem to be no need to quote it. But using grep on my quicklisp directory finds numerous instances of '(), written by…
Greg Buchholz
  • 860
  • 4
  • 16
5
votes
4 answers

Checking for lists of empty values

What is the most effective way to check if a list contains only empty values (not if a list is empty, but a list of empty elements)? I am using the famously pythonic implicit booleaness method in a for loop: def checkEmpty(lst): for element in…
Kracit
  • 1,100
  • 7
  • 11
1
2 3
14 15