In Python scipy.sparse, there are methods to convert between CSR, CSC, LIL, DOK, etc. implementations of a sparse matrix. What is the most efficient way in Mathematica to construct a `mxn`

`SparseArray`

from the LIL data? (inverse of this question)

More specifically, I have a list `ll={l1,...,ln}`

, where each `lv`

is of the form `{{u1,w1},...}`

, which means the matrix has an entry `{u,v}->w`

. Note that `lv`

may be *empty* (zero column). Note that `lv`

may have *repeated entries*, which should be summed (solution for this is here). For testing purposes, my cases are similar to the following example (e.g. millionXmillion matrix with 10 entries per column, all from the list R):

```
m=n=10^6; r=10; R={-1,1}; ll=Table(Transpose@{RandomInteger({1,m},r),RandomChoice(R,r)},n);
```

My current solution is:

```
SetSystemOptions("SparseArrayOptions"->{"TreatRepeatedEntries"->1});
LIL(ll_,m_,n_) := Module({l,uu,vv,ww}, l=Length/@ll; If(Plus@@l==0,Return@SparseArray({},{m,n}));
vv=Flatten(Table(ConstantArray(v,l((v))),{v,n}),1); {uu,ww}=Transpose@Flatten(ll,1);
SparseArray(Transpose({uu,vv})->ww) );
AbsoluteTiming(LIL(ll,m,n);)
```

{5.07803,Null}

Is there a better way? What about parallelization? How could I compile this code? (the matrix entries are integers or rationals)

P.S. Let me just mention that in Python, I haven’t yet found a library for sparse matrices that allows rational number entries (exact fractions). Also, when I set every second column and every second row in a matrix to 0, the scipy.sparse implementation is waaay slower than Mathematica’s SparseArray (by a factor of 100). So I’m incredibly happy we have this data structure implemented in Mathematica in such an efficient way.