What is a list called in programming?


List is the most versatile data type available in functional programming languages used to store a collection of similar data items. The concept is similar to arrays in object-oriented programming. List items can be written in a square bracket separated by commas. The way to writing data into a list varies from language to language.

Program to Create a List of Numbers in Java

List is not a data type in Java/C/C++, but we have alternative ways to create a list in Java, i.e., by using ArrayList and LinkedList.

The following example shows how to create a list in Java. Here we are using a Linked List method to create a list of numbers.

import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class HelloWorld { public static void main (String[] args) throws java.lang.Exception { List<String> listStrings = new LinkedList<String>(); listStrings.add("1"); listStrings.add("2"); listStrings.add("3"); listStrings.add("4"); listStrings.add("5"); System.out.println(listStrings); } }

It will produce the following output −

[1, 2, 3, 4, 5]

Program to Create a List of Numbers in Erlang

-module(helloworld). -export([start/0]). start() -> Lst = [1,2,3,4,5], io:fwrite("~w~n",[Lst]).

It will produce the following output −

[1 2 3 4 5]

List Operations in Java

In this section, we will discuss some operations that can be done over lists in Java.

Adding Elements into a List

The methods add(Object), add(index, Object), addAll() are used to add elements into a list. For example,

ListStrings.add(3, “three”)

Removing Elements from a List

The methods remove(index) or removeobject() are used to remove elements from a list. For example,

ListStrings.remove(3,”three”)

Note − To remove all elements from the list clear() method is used.

Retrieving Elements from a List

The get() method is used to retrieve elements from a list at a specified location. The getfirst() & getlast() methods can be used in LinkedList class. For example,

String str = ListStrings.get(2)

Updating Elements in a List

The set(index,element) method is used to update an element at a specified index with a specified element. For Example,

listStrings.set(2,”to”)

Sorting Elements in a List

The methods collection.sort() and collection.reverse() are used to sort a list in ascending or descending order. For example,

Collection.sort(listStrings)

Searching Elements in a List

The following three methods are used as per the requirement −

Boolean contains(Object) method returns true if the list contains the specified element, else it returns false.

int indexOf(Object) method returns the index of the first occurrence of a specified element in a list, else it returns -1 when the element is not found.

int lastIndexOf(Object) returns the index of the last occurrence of a specified element in a list, else it returns -1 when the element is not found.

List Operations in Erlang

In this section, we will discuss some operations that can be done over lists in Erlang.

Adding two lists

The append(listfirst, listsecond) method is used to create a new list by adding two lists. For example,

append(list1,list2)

Deleting an element

The delete(element, listname) method is used to delete the specified element from the list & it returns the new list. For example,

delete(5,list1)

Deleting last element from the list

The droplast(listname) method is used to delete the last element from a list and return a new list. For example,

droplast(list1)

Searching an element

The member(element, listname) method is used to search the element into the list, if found it returns true else it returns false. For Example,

member(5,list1)

Getting maximum and minimum value

The max(listname) and min(listname) methods are used to find the maximum and minimum values in a list. For example,

max(list1)

Sorting list elements

The methods sort(listname) and reverse(listname) are used to sort a list in ascending or descending order. For example,

sort(list1)

Adding list elements

The sum(listname) method is used to add all the elements of a list and return their sum. For example,

sum(list1)

Sort a list in ascending and descending order using Java

The following program shows how to sort a list in ascending and descending order using Java −

import java.util.*; import java.lang.*; import java.io.*; public class SortList { public static void main (String[] args) throws java.lang.Exception { List<String> list1 = new ArrayList<String>(); list1.add("5"); list1.add("3"); list1.add("1"); list1.add("4"); list1.add("2"); System.out.println("list before sorting: " + list1); Collections.sort(list1); System.out.println("list in ascending order: " + list1); Collections.reverse(list1); System.out.println("list in dsending order: " + list1); } }

It will produce the following output −

list before sorting : [5, 3, 1, 4, 2] list in ascending order : [1, 2, 3, 4, 5] list in dsending order : [5, 4, 3, 2, 1]

Sort a list in ascending order using Erlang

The following program shows how to sort a list in ascending and descending order using Erlang, which is a functional programming language −

-module(helloworld). -import(lists,[sort/1]). -export([start/0]). start() -> List1 = [5,3,4,2,1], io:fwrite("~p~n",[sort(List1)]),

It will produce the following output −

[1,2,3,4,5]

Updated: 05/02/2021 by Computer Hope

List may refer to any of the following:

What is a list called in programming?

1. With some computer programming languages, a list is another name for an array.

2. A list is any information displayed or organized in a logical or linear formation. Below is an example of a numerical list, often used to show several steps that need to be performed to accomplish something.

  1. First step.
  2. Second step.
  3. Third and final step.

Data structure, Inventory, Item, Multilevel list, Numbering, To-do list, Unordered list, Whitelist

What is a list called in programming?

Programming basics[1]

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].[2]

Please be careful as lists can be thought of as analogous to arrays, but they aren't the same thing. Arrays traditionally have a fixed memory size whilst lists have dynamic memory allocation. When you are working in Python, call a list a list. When you are working in PHP or Javascript, call an array an array. When you are working in C, call for help!

List Index[edit]

What is a list called in programming?

Lists, like arrays, are indexed starting at zero. The first element in a list is the "zero-th" element. In the example above (thank you for permission to use this image, programiz.com) [3] we can see also negative numbers, which are helpful if you want to select (or slice) a range of elements from a list.

The first element is in position zero, the second element is position one, and so on...

List slicing[edit]

This answer on stack overflow is a superb overview of slicing[4]:

a[start:stop:step] # items start through stop-1 step a[start:] # items start through the rest of the array a[:stop] # items from the beginning through stop-1 a[:] # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1] # last item in the array a[-2:] # last two items in the array a[:-2] # everything except the last two items

Similarly, step may be a negative number:

a[::-1] # all items in the array, reversed a[1::-1] # the first two items, reversed a[:-3:-1] # the last two items, reversed a[-3::-1] # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Creating a list[edit]

# The code below creates a list named polishAnimals polishAnimals = ['Bison', 'Moose', 'Deer', 'Lynx', 'Wolf', 'Beaver', 'Otter']

Accessing a list[edit]

# If you want to print a list (kind of ugly) you can simply: print(polishAnimals) # However, it is far more common to slice into a list # The code below accesses the 2nd item in the list named 'polishAnimals' print(polishAnimals[2]) # there is a lot more to slicing in Python.

Inserting into a list[edit]

# if we want to add onto a list (append) we could simply use the append method. The code below appends wild board onto the end of our list. polishAnimals.append('wild boar') # if we wanted to replace a certain element, we could simply overwrite it. Below we are replacing the 2nd item of our list with a new animal: polishAnimals[1] = 'Stork'

Deleting an element from a list[edit]

# There are a few different ways to remove an element from a list. The first way is to call the remove method. # The remove method works by finding the name of an element. polishAnimals.remove('Moose') # another way to remove an element from a list is to call the pop method. Pop-ing removes an index and return the value you removed. # the line below removes the 2nd element from our list and makes it available for us to use. polishAnimals.pop(1) # we might use the pop method like this: animalsDeleted = polishAnimals.pop[1] # we can also use the del function. del polishAnimals[2] # to delete an entire list, call the clear method (this is only available in Python 3+ polishAnimals.clear

Standards[edit]

  • Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.

References[edit]