A list is a collection of similar or different types of data elements.
Dynamic Language like python can store different data types in the same list, but for statically typed language like C++, a list means a collection of similar data types. Every list item can be accessed by its indices, and for most of the languages (python, java, C++, etc.) indices start from 0.
An array is the same as the list for dynamic language, but for a language like C++ where for a list dynamic memory allocation is done by the compiler, for array user has to do it manually.
Implementation of List inside List using C++:
C++
// C++ code to create a list inside another list #include <bits/stdc++.h> using namespace std; int main() { // Inside this list the another list // will be created/inserted. // Also ListofList only can store // vector<int> datatype, unlike // python which can store anything. list<list< int > > ListofList; // The list to be inserted, // normal integer type list. list< int > insertedList; for ( int i = 1; i < 5; i++) { insertedList.push_back(i); } // Pushing insertedList inside ListofList ListofList.push_back(insertedList); ListofList.push_back(insertedList); for ( auto it = ListofList.begin(); it != ListofList.end(); it++) { for ( int j : *it) cout << j << " " ; cout << endl; } return 0; } |
Java
// Java code to create a list inside another list import java.util.*; class GFG{ public static void main(String[] args) { // Inside this list the another list // will be created/inserted. // Also ListofList only can store // Vector<Integer> datatype, unlike // Java which can store anything. ArrayList<ArrayList<Integer> > ListofList = new ArrayList<ArrayList<Integer> >(); // The list to be inserted, // normal integer type list. ArrayList<Integer> insertedList = new ArrayList<Integer>(); for ( int i = 1 ; i < 5 ; i++) { insertedList.add(i); } // Pushing insertedList inside ListofList ListofList.add(insertedList); ListofList.add(insertedList); for (ArrayList<Integer> it : ListofList){ for ( int j : it) System.out.print(j+ " " ); System.out.println(); } } } // This code is contributed by shikhasingrajput |
C#
// C# code to create a list inside another list using System; using System.Collections.Generic; public class GFG{ public static void Main(String[] args) { // Inside this list the another list // will be created/inserted. // Also ListofList only can store // List<int> datatype, unlike // C# which can store anything. List<List< int > > ListofList = new List<List< int > >(); // The list to be inserted, // normal integer type list. List< int > insertedList = new List< int >(); for ( int i = 1; i < 5; i++) { insertedList.Add(i); } // Pushing insertedList inside ListofList ListofList.Add(insertedList); ListofList.Add(insertedList); foreach (List< int > it in ListofList){ foreach ( int j in it) Console.Write(j+ " " ); Console.WriteLine(); } } } // This code is contributed by shikhasingrajput |
1 2 3 4 1 2 3 4
Implementation of List inside List using Python:
For Dynamic Language like python Create 2 or more lists and append them inside one of the lists, And it’ll create a list inside a list,
Python3
list_1 = [ 1 , 2 , 3 , 4 ] list_2 = [ 4 , 7 , 8 , 9 ] list_3 = [ 3 , 1 , 4 , 7 ] list_4 = [ 3 , 5 , 7 , 11 ] # list_2, list_3 and list_4 # have been appended inside list_1 list_1.append(list_2) list_1.append(list_3) list_1.append(list_4) # As List in python can store different data types, # that's why here it able to store # integer type and List in a same list. print (list_1) |
Javascript
<script> // JavaScript code to create a list inside another list let list_1 = [1, 2, 3, 4]; let list_2 = [4, 7, 8, 9]; let list_3 = [3, 1, 4, 7]; let list_4 = [3, 5, 7, 11]; // list_2, list_3 and list_4 // have been appended inside list_1 list_1.push(list_2) list_1.push(list_3) list_1.push(list_4) // As List in python can store different data types, // that's why here it able to store // integer type and List in a same list. document.write(list_1) // This code is contributed by rakeshsahni </script> |
[1, 2, 3, 4, [4, 7, 8, 9], [3, 1, 4, 7], [3, 5, 7, 11]]
Creating array inside the array in C:
But for statically typed language like C one have to predefine everything like the data type of the array which stores another array.
For example, to create a 1D integer array inside another array, firstly, the main array should store (int*) datatype then only, another 1D array can be stored/created inside that array.
Note: Following the same way an array can be created inside another array in C++ also.
C++
// C++ code to create array inside array #include <bits/stdc++.h> using namespace std; #define size 5 int main() { // Here is an array(dynamically allocated) // that can store array(1-D), // or (int*) type data int ** out_array = ( int **) malloc (size * sizeof ( int *)); // Here is an normal integer array // (dynamically allocated), which stores // (int) datatype int * temp1 = ( int *) malloc (size * sizeof ( int )); int * temp2 = ( int *) malloc (size * sizeof ( int )); for ( int i = 0; i < size; i++) { temp1[i] = i; // 0, 1, 2, 3, 4 temp2[i] = i; } // Now Creating array inside array // by storing int* datatype // inside the out_array out_array[0] = temp1; out_array[1] = temp2; cout << "After Creating array inside array " << endl; // Since there are only two // array inside the out_array for ( int i = 0; i < 2; i++) { for ( int j = 0; j < size; j++) { cout << out_array[i][j] << " " ; } cout << endl; } return 0; } |
C
#include <stdio.h> #include <stdlib.h> #define size 5 int main() { // Here is an array(dynamically allocated) // that can store array(1-D), or // (int*) type datat int ** out_array = ( int **) malloc (size * sizeof ( int *)); // Here is an normal integer array // (dynamically allocated), which // stores (int) datatype int * temp1 = ( int *) malloc (size * sizeof ( int )); int * temp2 = ( int *) malloc (size * sizeof ( int )); for ( int i = 0; i < size; i++) { temp1[i] = i; temp2[i] = i; } // Now Creating array inside array // by storing int* datatype // inside the out_array out_array[0] = temp1; out_array[1] = temp2; printf ( "After Creating array inside array\n" ); // Since there are only two arrays // inside the out_array for ( int i = 0; i < 2; i++) { for ( int j = 0; j < size; j++) { printf ( "%d " , out_array[i][j]); } printf ( "\n" ); } return 0; } |
Java
// Java code to create array inside array class GFG{ static final int size = 5 ; public static void main(String[] args) { // Here is an array(dynamically allocated) // that can store array(1-D), // or (int*) type data int [][]out_array = new int [size][size]; // Here is an normal integer array // (dynamically allocated), which stores // (int) datatype int []temp1 = new int [size]; int []temp2 = new int [size]; for ( int i = 0 ; i < size; i++) { temp1[i] = i; // 0, 1, 2, 3, 4 temp2[i] = i; } // Now Creating array inside array // by storing int* datatype // inside the out_array out_array[ 0 ] = temp1; out_array[ 1 ] = temp2; System.out.print( "After Creating array inside array " + "\n" ); // Since there are only two // array inside the out_array for ( int i = 0 ; i < 2 ; i++) { for ( int j = 0 ; j < size; j++) { System.out.print(out_array[i][j]+ " " ); } System.out.println(); } } } // This code is contributed by shikhasingrajput |
After Creating array inside array 0 1 2 3 4 0 1 2 3 4
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!