List is a data structure that stores elements in an ordered and sequential manner. A list can store repetitive elements which means a single element can occur more than once in a list.
Takeaways
Subsetting is accessing the part of a list from any start index to the end index.
What is a List?
List is an ordered data structure that is used to store different or same elements in a sequential manner.
The list is similar to array in data structure but in the array, we can store only the elements which are of same data-type whereas in the list in the data structure, with some of the programming languages like python and javascript we can store elements with different data types also.
Let’s see an example for array and list to know the difference better.
Javascript
list in javascript : -
let list1 = ['scaler',234,78,true]
C++
array in c++ : -
int arr = [23,45,78,32,65,78]
As seen above in the example, list1 in javascript is storing an integer, boolean as well as string in its list data structure.
Whereas the array in C++ above is storing only integer elements.
There is no need to define static size to the list i.e we can add more elements to the list dynamically and as the new elements get added to the list, the size of the list increases dynamically.
Sub-setting
Subsetting is extracting a part of the list. We can access the list elements through the indexes.
Let’s first see an example of accessing an element of the list in python.
index - 0 1 2 3
list2 = ["scaler",45,65,true]
The above list named list 2 contains strings, integers, and boolean values.
Each element is present at a particular index in the list as shown in the above list (list2). These indexes are used to access the elements of the list.
Suppose we want to access element 65 from the list then it can be accessed by: list2[2]
Similarly, list2[0] = “Scaler”
list[1] = 45
list[3] = true
Index in a list data structure starts from 0. Now, if we want to access any part of the list together then we use the subsetting concept.
For example, if we want to access [45,65, true] from the list2 then we can do that by list2[1:4] in python.
list2[1:4] – It starts picking the element in list2 from index 1 and stops at index just before 4 i.e 3. The synatx for subsetting in python is :
list_name[start_index,end_index+1]
The syntax given above starts picking the elements from start_index and goes till end_index.
We can also use negative index to get the last element of the list directly in python.
So, list2[-1] = true
Operation on List Data Structure
There are various operations that can be performed on the list data structure. some of the basic operations that are mostly performed on list data structure are :
- Add or insert operation – We can add more elements to the list. The elements added can be of any data type in the case of python and javascript. Operations such as list. append() are used in python to insert elements to the list. Also, many languages use list.push() operation to insert new elements to the list.
- Replace or reassign operation – using replace and reassign operations we can replace an already existing element in the list with another element. we make use of the index of the list elements to replace or reassign any element.
- Delete or remove operations – By delete and remove operation we can easily delete or remove any list elements. list. pop(), list. pop(index) are some of the operations used in python and other languages to remove an element from that particular index or the end of the list.
- Find or lookup or search operations – If we want to search and fetch any element present in the list then we can use find and lookup or search operations on the list.
Usage of the List Data Structure in Different Programming Languages
the list data structure is used a lot in real-life applications where data need to be stored in a sequential and ordered manner.
We can implement CRUD – Create, Read, Update and delete operations on the list data structure in different programming languages.
CRUD Operation on the List in Python :
# initializing an empty list to store all balanced diet constituents
balanced_diet = list()
# function to add or insert balanced diet constituents to the balanced_diet list
def add_balanced_diet_constituents()
constituent = input("enter the constituent you want to add to your balanced diet");
balanced_diet.append(constituent)
# function to delete/remove balanced diet constituents from the balanced_diet list.
def delete_balanced_diet_constituents()
constituent = input("Add the constituent that you want to remove from the balanced_diet list")
balanced_diet.remove(constituent)
# function to update balanced diet constituents in the balanced_diet list
def update_balanced_diet_constituents()
old_constituent = input("enter the constituent that you want to update in balanced_diet")
new_constituent = input("enter the new constituent you want to update the old_constituent with")
for index, constituent in enumerate(balanced_diet):
if old_constituent == constituent:
balanced_diet[index] = new_constituent
break
# function to print all balanced diet constituents from the balanced_diet list.
def printAllElements()
print("balanced diet constituents are :")
for constituent in balanced_diet:
print(constituent, end=' ')
CRUD Operation on List in Java :
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
class listClass {
static List<String> balanced_diet = new ArrayList<String>();
static Scanner sc = new Scanner(System.in);
// function to add or insert balanced diet constituents to the balanced_diet list
public static void add_balanced_diet_constituents(){
String constituent = null;
System.out.println("Enter the constituent to be added in the balanced_diet list : ");
constituent = sc.nextLine();
balanced_diet.add(constituent);
}
// function to delete/remove balanced diet constituents from balanced_diet list.
public static void delete_balanced_diet_constituents(){
String constituent = null;
System.out.println("Enter the constituent to be deleted from the balanced_diet object : ");
constituent = sc.nextLine();
balanced_diet.remove(constituent);
}
// function to update balanced diet constituents in balanced_diet list
public static void update_balanced_diet_constituents(){
String old_constituent = null;
String new_constituent = null;
System.out.println("Enter the old constituent that needs to be updated : ");
old_data = sc.nextLine();
System.out.println("Enter the new constituent that will be updated : ");
new_constituent = sc.nextLine();
int index_of_old_constituent = 0;
index_of_old_constituent = balanced_diet.indexOf(old_constituent);
balanced_diet.set(index_of_old_constituent,new_constituent);
}
// function to print all balanced diet constituents from the balanced_diet list
public static void print_all_constituents(){
System.out.println();
System.out.println("The constituents in the balanced_diet list Object is : ");
for(int index=0; index<balanced_diet.size(); index++){
System.out.print(balanced_diet.get(index));
System.out.print("\n");
}
System.out.println();
}
}
CRUD Operation on List in C++ :
#include <iostream>
#include <list>
#include <string>
#include <iterator>
using namespace std;
list<string> balanced_diet;
// function to add or insert balanced diet constituents to the balanced_diet list
void add_balanced_diet_constituents(string constituent){
balanced_diet.push_back(constituent);
}
// function to delete/remove balanced diet constituents from balanced_diet list.
void delete_balanced_diet_constituents(string constituent){
list<string> :: iterator it = balanced_diet.begin();
while(1){
if(*it==constituent){
balanced_diet.erase(it);
break;
}
++it;
}
}
// function to update balanced diet constituents in balanced_diet list
void update_balanced_diet_constituents(string old_constituent, string new_constituent){
for (auto constituent = balanced_diet.rbegin(); it != balanced_diet.rend(); constituent++) {
if (*constituent == old_constituent) {
*constituent = new_constituent;
}
}
}
// function to print all balanced diet constituents from the balanced_diet list
void print_balanced_diet_constituents(){
list<string> :: iterator constituent;
for(constituent = balanced_diet.begin(); constituent!= balanced_diet.end(); ++constituent){
cout << *constituent;
cout<< std::endl;
}
cout << '\n';
}
CRUD operation on List in Javascript :
const balanced_diet = [];
var idx = 0;
// function to add or insert balanced diet constituents to the balanced_diet list
function add_balanced_diet_constituents(constituent){
balanced_diet[idx] = constituent;
idx++;
}
// function to delete/remove balanced diet constituents from balanced_diet list.
function delete_balanced_diet_constituents(constituent){
var constituent_index = balanced_diet.indexOf(constituent);
if (constituent_index > -1) {
balanced_diet.splice(constituent_index, 1);
}
}
// function to update balanced diet constituents in balanced_diet list
function update_balanced_diet_constituents(old_constituent, new_constituent){
var old_constituent_index = car_names.indexOf(old_constituent);
balanced_diet[old_constituent_index]= new_constituent;
}
// function to print all balanced diet constituents from the balanced_diet list
function print_all_constituents(){
console.log(balanced_diet);
}
Conclusion
- A list in the data structure is an ordered data structure that stores elements sequentially and can be accessed by the index of the elements.
- A list in the data structure can store different or same data types elements depending on the type of programming language that is being used.
- We can perform add/insert, delete/remove, update and search/lookup operations on a list in the data structure.