Listado de la etiqueta: Lists

In C# programming, lists are used for storing and processing different data. We can perform different tasks on these lists. One of them is combining two different lists. In C# programming, combining two lists means joining or merging two different lists into one new list. We have different methods to combine lists in C#. We will use three different methods to combine two lists in C# programming. Here, we are going to perform different examples in Ubuntu 20.04.

Methods for Combining Lists in C# Programming

AddRange() Method


List1_name .AddRange (list2_name);

Foreach Loop Method


List2_name .foreach (Any_list => list1_name.Add(Any_List));

Enumerable.Concat() Method


List1_name .Concat (list2_name);

Example # 1: By Using the AddRange() Method

We will demonstrate an example in which we use the AddRange() method for combining two different lists in C# programming. We perform the given examples in Ubuntu 20.04 text editor. When using the Ubuntu 20.04 text editor, we must save our file with the “.cs” extension. Then, explain the following code in detail:

We start our program with the “using System” library. We use this library to access classes and functions. It provides us with many valuable functions and classes. The “System.Collections.Generic ” is here for good performance containing different interfaces and classes which define generic collections. After invoking the “main” function, we create a new list named “Vegetables”, which is a string data type. We use the “new” keyword for creating a new list. When this new list is created, we have to pass some string data to this list. We add “Potato”, “Chilli”, “Garlic”, and “Tomato” to this list.

Now, we have to create another list so that we can combine both lists. So, we will form another list with the name “Fruits”. We created this list with the same method as we created the first one. When the second list is created, we add some fruits’ names to this list. Here, you can see that we add “Apple”, “Mango”, “Banano” and “Orange” to the second list. Now, it’s time to add both lists using the “AddRange” method. We use the same syntax of the AddRange() method previously discussed. Here, we give the name of the first list, which is “Vegetables”. Then, use the AddRange method and the name of the second list, “Fruits”.

In this code, the line “Vegetables.AddRange(Fruits)” is used for combining these two lists. After this, we will print this combined list with the help of “Console.WriteLine” statements. First, this statement prints the line. Then, we use String. Join (“,”, Vegetables) inside the Console.WriteLine statement to print the combined list. In this, “Join()” is a method to combine list elements using “,” to separate each element of the list.

As we use the Ubuntu 20.04 to perform these examples, we have to run some commands for the output on the terminal of Ubuntu 20.04. First, you must launch the Ubuntu terminal and write the “mcs” command. It is used for the compiling of our source code. When using this “mcs” command, we put the “.cs” extension with the file_name. You have to press Enter to run this “mcs” command. When this command runs successfully and there is no error in our code, it creates an executable file for us.

After that, we use another command which is the “mandril” command here. This command is used for the execution of our code. When we use this command, we use the “.exe” file extension. Press Enter to run this “mandril” command. The output of the code is rendered on the following screen:

In this output, you can see that it prints the two lists in a signal list, which means it combines both lists in one list and displays both lists in a single line or a single list.

Example # 2: By Using ForEach Loop Method

Now, we explore another example in which we use the ForEach Loop method to combine two lists. We perform different examples by using different methods for combining two lists.

The “Using System” is here, which contains classes and functions. Then, we have “System.Collections.Generic”, which contains interfaces and classes. The class is public with the name “Program”. We must have the “Main” function in our program. Then, we have to create two different and separate lists. We create the first list with the name “Stationery” by using the “new” keyword. We must add some elements to this list. So, we add “Pencil”, “Marker”, “Eraser”, and “Color” to this “Stationery” list.

After creating the first list, we create another list of “Subjects” and add different subject names to this second list. Here, we add “English”, “Computer”, “Biology”, and “Mathematics” to this “Subject” list. Now, we have to merge these two lists using the “ForEach” Loop method. When we use this ForEach loop method, we must follow the syntax of this method. First, we have to give the name of the second list and use the ForEach loop. Inside this ForEach loop, we give the name of any list with a fat arrow “=>”.

After this fat arrow gives the name of the first list and then puts “Add” and inside add again gives the name of any list. In this code “Subject.ForEach(Things => Stationery. Add (Things)” line is used to merge two lists using the ForEach loop method. Now, we print this merged list by using “Console.WriteLine”. We print this list with the same method we deliberated in our preceding example. The output of our code is provided in the image provided below:

Example # 3: By Using Enumerable.Concat() Method

We are performing this given example for you to easily learn how to use different methods to combine two lists.


Here, we create a list of the variable named “listA” by using the “new” keyword and a list of string data types. We add different elements using the “list_name.Add()” method. The name of the first list is “listA” here. We add “Computer”, “Keyboard”, and “Mouse” to this list by using the “Add ()” method. We will print the list first in this code and combine both lists after printing both lists separately. We print the first list using the foreach loop. We initialize a variable with the name “e”, which stores all the elements of the “listA” and prints all elements using the “Console.WriteLine” method.

After printing this first list, we create and print a new list name, “listB” with the same method that we used to create and print the first list, “listA”. When both lists are created, we combine these lists. And for combining, we use the third method, which is “Enumerable.Concat()”. We create another list named “FinalList” and initialize this with the “Enumerable.Concat()” method.

First, we give the name of the first list, “listA” then “Concat” inside this, we give the name of the second list, which is “listB” and the ToList(). The ToList() gets the elements from the list and returns the new list. After all this, we print the concatenate or combined list using the same method we used in our previous examples. The output of this code is in the following image:


This tutorial discussed how to combine two lists in C# programming in Ubuntu 20.04 by using different methods. This tutorial explained three different methods for combining two lists. We have also provided screenshots of the codes along with the output of each code for your better understanding. In addition, we utilized three different examples in which we use three different methods for merging or combining two lists in C# programming. I hope you will easily learn this concept, and this tutorial will be helpful for you in the future.

Source link

Lists are similar to dynamically allocated arrays, declared in other languages. Lists do not always have to be homogeneous, making Python’s most powerful feature. Integers, Strings, and Objects can all be found in a single list. Lists are changeable, which means they can be changed after they are created.

In Python, lists are ordered and counted. Every member inside the list has its separate position in the list, allowing duplication of the list’s elements while maintaining the credibility of each member. Lists help store and iterate through an iteration of data.

How to Create it?

This artifact will teach you to create and initialize a Python list of lists. A list of lists is a nested list with one or more lists. There are numerous methods for creating a list of lists. A different method will be used to construct a list of lists. Then, we will explore how to access list items after we’ve created a list of lists. Let’s look at a few examples.

Example 1: Using the append() Function to Create a List of Lists in Python in Ubuntu 20.04

The List append() function in Python is used to append and add items to the end of a List. In this illustration, we are just adding a list to a list as an item using the append() method.

Let’s see the implemented python code in the following image. In our first step, we created two list arrays represented with “list_x” and “list_y.” The lists here are initialized with different integer values.

After that, we have created a new list as “list_z,” which is initially empty. We are going to add the above lists to that list. Then, we have called the append function for the “list_z,” to which we want to append the above-initialized lists. The append function takes the list_x and the list_y as an argument individually. This append method will add the whole list in the list_z along with the elements of the list. The print function is called for printing the lists of newly generated “list_z.”

list_x = [2, 4, 6, 8]

list_y = [3, 5, 7, 9]

list_z = []




The lists are appended to the one list as shown on the output terminal screen.

Example 2: Using the List Initializer to Create a List of Lists in Python in Ubuntu 20.04

An alternative way to make a list in Python, use the list initializer syntax. We can use this approach to make a list of lists by providing lists to the list initializer as elements.

At the beginning of the python code below, we have defined two lists separately. The lists are represented as “list1” and “list2”. These lists are stored with some integral values, as shown. Then, we have the list initializer method for which we have declared a new list as “list3”. We have initialized the “list3” with the “list1” and “list2”.

Here, We treat lists as items by using a list initializer. By using lists as items, we can make a list of lists. It’s the most straightforward method for making a list of lists. The list3 will be printed, which has both the above-specified lists.

list1 = [21, 22, 23, 24]

list2 = [25, 25, 27, 28]

list3=[list1, list2]


The output here shows the list of lists in the prompt shell as follows.

Example 3: Using the for-loop

We will create a more comprehensive list of lists by explicitly utilizing the append() technique with the for loop.

In the following Python script, we have established an empty list. The list is given the name “mylist.” Then, we have a for loop for creating the list of lists. We have defined the range of the lists in the list by using the range method and passed the value “3” as an argument. The for loop has the variable “m,” which will iterate over the range defined for the list.

After that, we have called the append method, which will append the three lists in the list. Now, the for loop is invoked again for adding the elements in the lists in the range of “6”. The print function will print the three lists in the list and the elements in the lists.

mylist = []

for m in range(3):


for n in range(6):

mylist[m] .append(n)


The output is as follows:

Example 4: Using the List Comprehension to Create a List of Lists in Python in Ubuntu 20.04

In Python, list comprehension is a simple but elegant approach to generating lists. We generate lists using for loops and logic enclosed in square brackets using this strategy.

First of all, we have defined a list that now has string elements. Then, we created another list with the name “colors.” The list “colors” is an empty list for the time being. After that, we called the list “colors” and applied the for loop cycle to it. The for loop iterates over each item in the list and adds the elements in the list by creating a new variable, “elements.” The list “colors” of the lists will be printed at the end.

list = [‘pink’,‘red’,‘white’,‘blue’,‘green’]

colors = []

colors = [[elements] for elements in list]


We can also create a list of lists in python through this approach. This one is the shortest method among all.

Example 5: Accessing the Elements From the List of Lists in Python

By utilizing an index, we may access elements. The list index begins with 0 and ends with n-1, whereby n is the list length.

The following python program has a list initialized with some string values. The new empty list is also created as Animals. Then the for loop is applied to the list “Animal.” We are adding the list in the list “Animals” by using a for loop. In the end, we are displaying the list and accessing the list’s elements.

list = [‘cat’,‘dog’,‘hen’,‘parrot’, ‘panda’]

Animals = []

Animals = [[items] for items in list]





The list of lists is created and also accessed the list elements by the index location. The output is as follows.


Summing up! About creating a Python list of lists. A list of lists is a list where every value is a separate list. We have deeply gone through creating the list of lists in python. We have four different approaches that will create a list of lists in python easily and efficiently. Then, we have also accessed the list of list elements in python by the index position of the elements in the list. We hope you found it informative.

Source link