Listado de la etiqueta: List


This article will discuss different methods of removing duplicate items from a list in the C# programming language. These methods will be very useful in detecting and removing redundancy when adding and storing data in a list. The use of different C# libraries such as LINQ and collections.generic will also be discussed in this article. The most effective method to remove duplicates is the Distinct() and ToList() method, which eliminates all duplicates in one go and creates a list with unique elements. This method is present in the LINQ library of the C# Programming language.

The Distinct() Method

We use the Distinct() method to distinguish between items or variables. The LINQ library provides the Distinct method, this functionality to compare items or variables in the C# programming language as it is a query-based library. This method only removes duplicates from a single data source and returns the unique items into a new data source that would be a list. In our case, we will be using this method for the List class, so we will also add the ToList() method with the Distinct() method so that when the distinct items are recognized, they can be added to a new list.

The following is the syntax for writing this method in the C# programming language:

# “list name = list.Distinct().ToList();”

As can be seen, the method is used while creating a new list since it returns elements from an existing list to create a unique list. When initializing a list using this method, we must use the old list before calling the method for the inheritance of previous items of the old list.

Now that we know about the syntax, we will implement some examples and test this method with different data types of items in the C# programming language.

Example 01: Using the Distinct().ToList() Method to Remove Numbers From a List in Ubuntu 20.04

In this instance, we will be using the Distinct().ToList() method to remove numbers from an integer list in the C sharp programming language. We will first call the LINQ library, which has the Distinct().ToList() method so that it can be used further in the program. We will be transforming a list with duplicate entries and making a new list with unique values with the help of the distinct method. This method will be performed in the Ubuntu 20.04 environment.

Text Description automatically generated

In the prior C# program, we created an integer data type list and then used the system’s Add() function to add some items to it. We’ll make a new list and apply values to it using the “Distinct().ToList()” function, which will eliminate all of the duplicates. On the output screen, the list with the unique objects will be printed.

After compiling and executing the above program, we will get the following output as shown in this snippet below:

Text Description automatically generated

In the above output, we can see that all the entries of the list that were printed are unique and there are no duplicate items, and we have successfully removed duplicates from the list.

Example 02: Using the Distinct().ToList() Method to Remove Alphanumeric String From a List in Ubuntu 20.04

In this illustration, we will use the “Distinct().ToList()” method to remove duplicates from a string data type list, but the members of the list will be Alphanumeric characters to observe how the “Distinct().ToList()” method adapts. We will use the add function in the system to repeat the process of initializing a list.Library of collections. The function “Distinct().ToList()” creates a new list with unique entries. Due to its distinctiveness, the new list would then be utilized for future preference.

Text Description automatically generated

In the preceding C# code, we created a string data type list and then used the Add() function from the “system.collection” package to add some alphanumeric values to it. We will make a new list and apply values to it with the “Distinct().ToList()” method, which will eliminate all of the duplicates. On the output screen, the list with the unique objects will be printed.

After compiling and running the given C# code, we will get the following result, as seen in the image below:

Text Description automatically generated

We can see that all of the entries in the printed list are unique, and there are no duplicates, indicating that the Distinct function was effective in eliminating duplicates from the list.

After this, we will be looking into some different approaches to remove duplicates from a list in the C# programming language.

Using the Hash Set Class to Remove Duplicates in Ubuntu 20.04

In this method, we will be using the hash set class two to remove duplicates from a list using an object of the class and adding it to a new list. The hash set is a data set that only contains unique items from the “System.Collections.Generic” namespace. We will use the hash set class and create a new list in which there will be no duplicates due to the unique property of the hash set.

Text Description automatically generated

In the above C# program, we have initialized an integer data type list and assigned some numeric values to it. Then we created an object of the hash set class, which we then used in the value assigning of a new list so that it would have distinct values when it is printed using the display list function.

The output after compiling and executing this program is shown below:

Text Description automatically generated

As we can see in the output that the new list we created with the Hash set object has no duplicates as the add shared object successfully removed all the repetitive elements of the old list.

Using the IF Check to Remove Duplicates in Ubuntu 20.04

In this method, we will be using the traditional if check to verify that there are no duplicates present in the list. The if check will add only the unique items from the list and create a completely distinct list with no repetitions. We will use the foreach loop for traversing through the list to check for duplicates, not so for printing the new list with unique elements.

Text, letter Description automatically generated

In the above C# program, we have initialized a string data type list and assigned some text values to it with several duplicate items. Then we started a for each loop in which we nested an if check, and we added all the unique items to a new list that we initialized before starting the for each loop. After this, we started another for each loop in which we printed all the elements of the new list. The result of this C# program will be as shown below on the output screen.

Text Description automatically generated

As we can see in the output screen, all the elements of the new list are unique as compared to the old list, which had several duplicates. The if check eliminated all the duplicates from the old list and added them to the new list that we saw on the output screen.

Conclusion

In this article, we discussed several different approaches to removing duplicate items from the list data type of the C# programming language. The different libraries of the C# language were also used in these approaches as they provided different functions and methodologies to implement this concept. The Distinct method was discussed in great detail as it is a very effective and precise method to remove duplicates from a list in the C# programming language. To eliminate duplicates from the list, we utilized the hash set class and the standard IF check. All these approaches were implemented in the Ubuntu 20.04 environment to understand the different methods better.



Source link


C# list contains means we check whether the list contains the number or element which we want to check. It will check the specified element, string, or number in the list. If the list contains that number or element, it will return that number or element. It will also return true if the list has the stated element, and it will return false if the list does not hold the stated element. In this tutorial, we will discover the concept of a “list contain” in C# programming. We have different examples in this guide in which we will check the specified elements in the list.

Example # 1

We are going to perform the given examples in Ubuntu 20.04. In Ubuntu 20.04, first, we open the text editor and type the code which is given in the image below. After this, we save this with the file name of our choice and the extension “.cs”.

In this program, we use the “using” keyword, which will create an apelativo for a namespace. The “using System” is here to give us useful classes and functions. It means that we are using system libraries in our code. Then, we have “System.Collections.Generic” for using interfaces and classes. We use this namespace here for accessing the list. It will provide better performance. The “System.Linq” is used for retrieving the data.

We must have the “main” function in our C# program. It is the entry point of our C# program. Our code starts execution from here. Here “var” is a data type named “list”. The “new” is a keyword for creating a new list here. It will generate a new list of strings. Now we will add different string variables to our list with the help of the “Add” method. The “Add” is the method in C# for adding diverse elements to the list. The “list.Add” will add the given strings to our list. The “list.Add “(“Computer”)” will add the “Computer” to our list. Same as the “list.Add(“Laptop”)” and “list.Add(“printer”)” will add “Laptop” and “Printer” to our list, which we have created in this program.

After adding all these elements to our list, we will check whether our list contains a laptop or not. So, we use the “if” statement, which will check this. Inside this “if” statement, we use the “Contain” method, which returns a value that indicates whether the list contains “laptop” or not. Then, we have “Console.WriteLine” for printing. If the “if” statement is true, then it will print the line of code which is written below this “if” statement. Then we close all the brackets and save this code.

We perform this example in Ubuntu 20.04, so for output, we open the Ubuntu terminal and write the given commands on this terminal. The first command is the “mcs” command to compile our C# code. When using this “mcs” command, we must use the “.cs” file extension with the filename. If the code is error-free, then it will create an executable file for us with the same name. After this, we will execute this code with the help of the “simio” command. When using this “simio” command, we must use the file extension of “.exe” with the file name and hit enter. It will give the output of our code.

In this output, you can understand that it first compiles our code. As our code is error-free, then it executes our code and gives the output. It checks whether the list contains the specified element or not. Our list contains “laptop”, so it prints the line on the terminal screen.

Example # 2

Now, we will explore another example for you so that you can easily understand this concept with the help of different examples. In this instance, we use the list of integers. Look at the second example, which is given below in the image.

In this first line, we have the “using System” library for accessing classes and functions. Then we have “System.Collections.Generic” which allows us to create strongly typed collections. We created a class named “Csharp” and invoked a “main” function. The “string [] args” is a variable. Inside the “main” function, we create a list of integers. The “new” keyword creates a new list, and “int” is used for integers. We must have the integers values in our list. So, for adding integer numbers to our list, we will utilize the “Add” function. The “numbers.Add(33)” is used for adding “33” to our list. Here “numbers” represent the name of the list which we have created above, and “Add” will add “33” to the list. By using “numbers.Add(56)” and “numbers.Add(84)”, we will add “56” and “84” respectively.

After that, we have “bool” which is the Boolean data type, and it returns the “true” and “false” results of the given statement. We declare “num” with this “bool” data type and assign a “Contain” method to this “bool num”. The “numbers.Contains(56)” checks “56” in the list. Here “numbers” represent the list in which we check the given number. If the list contains “56” then this “bool num” will return “true”; otherwise, it will return “false”. We will also check “30” in this list by using the same method and will return “true” or “false”. We use the “Console.WriteLine” statement to print the result.

Here, in this output, it returns “True” when it checks “56” in the list because our list contains “56”. And it returns “false” in the case of “30”, as our list does not contain “30”.

Example # 3

Here, we have one more example of the C# “list contain”. In this code, first, we print all the list elements and then check the specified number in the list.

We have “using System” and “using System.Collections.Generic” which we discussed in detail in previous examples. Then we declare a public class called “Program” and invoke a “main” function inside this. Then we create a list of integers with the name “num” and add the numbers by using the “Add” method. First, we have to print all the numbers which we add to our list. This “Console.WriteLine” will print the line on the screen, then we use the “foreach” loop. Inside this “foreach” loop, we are going to initialize an integer “int” with the name “res” which reads the numbers in the list named “num” and stores each number in “res”.

After this, we will print all these numbers on the terminal by using “Console.WriteLine(res)”. Then, we are going to check “400” and “202” in the list and print the result on the screen using “Console.WriteLine”.

The output of this code is like that in the above image. First, it prints all the numbers on the list and then checks both numbers and return the “true” and “false” result.

Conclusion

This guide explains how to use the “list Contain” method in C# programming. We discussed the codes in detail. We explain every line of the code so that you can simply comprehend how it works in C# programming. We demonstrate three different examples for your better understanding of this concept. After reading this guide and performing these examples by yourself, you will be able to use this “list Contain” method in your complex codes of C# programming as well.



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 = []

list_z.append(list_x)

list_z.append(list_y)

print(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]

print(list3)

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):

mylist.append([])

for n in range(6):

mylist[m] .append(n)

print(mylist)

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]

print(colors)

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]

print(Animals)

print(Animals[1])

print(Animals[3])

print(Animals[4])

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

Conclusion

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