Why are Arrays invariant, but Lists covariant?

E.g. why does val list:List[Any] = List[Int](1,2,3) work, but val arr:Array[Any] = Array[Int](1,2,3) fails (because arrays are invariant). What is the desired effect behind this design decision? Answer Because it would break type-safety otherwise. If not, you would be able to do something like this: val arr:Array[Int] = Array[Int](1,2,3) val arr2:Array[Any] = arr arr2(0) = … Read more

Appending to one list in a list of lists appends to all other lists, too [duplicate]

This question already has answers here: List of lists changes reflected across sublists unexpectedly (17 answers) Closed 5 years ago. I’m getting mad with list indexes, and can’t explain what I’m doing wrong. I have this piece of code in which I want to create a list of lists, each one containing values of the … Read more

Concatenate all list content in one string in C#

How do I concatenate all content of a list in one string in C#? Answer Searching for this: List<string> list = new List<string>(); // { “This “, “is “, “your “, “string!”}; list.Add(“This “); list.Add(“is “); list.Add(“your “); list.Add(“string!”); string concat = String.Join(” “, list.ToArray()); AttributionSource : Link , Question Author : kartal , Answer … Read more

Merge lists that share common elements

My input is a list of lists. Some of them share common elements, eg. L = [[‘a’,’b’,’c’],[‘b’,’d’,’e’],[‘k’],[‘o’,’p’],[‘e’,’f’],[‘p’,’a’],[‘d’,’g’]] I need to merge all lists, that share a common element, and repeat this procedure as long as there are no more lists with the same item. I thought about using boolean operations and a while loop, but … Read more

Python and SQLite: insert into table

I have a list that has 3 rows each representing a table row: >>> print list [laks,444,M] [kam,445,M] [kam,445,M] How to insert this list into a table? My table structure is: tablename(name varchar[100], age int, sex char[1]) Or should I use something other than list? Here is the actual code part: for record in self.server: … Read more