Iterating over every two elements in a list [duplicate]

You need a pairwise() (or grouped()) implementation. def pairwise(iterable): “s -> (s0, s1), (s2, s3), (s4, s5), …” a = iter(iterable) return zip(a, a) for x, y in pairwise(l): print(“%d + %d = %d” % (x, y, x + y)) Or, more generally: def grouped(iterable, n): “s -> (s0,s1,s2,…sn-1), (sn,sn+1,sn+2,…s2n-1), (s2n,s2n+1,s2n+2,…s3n-1), …” return zip(*[iter(iterable)]*n) for … Read more

vector vs. list in STL

vector: Contiguous memory. Pre-allocates space for future elements, so extra space required beyond what’s necessary for the elements themselves. Each element only requires the space for the element type itself (no extra pointers). Can re-allocate memory for the entire vector any time that you add an element. Insertions at the end are constant, amortized time, … Read more

How to print out all the elements of a List in Java?

The following is compact and avoids the loop in your example code (and gives you nice commas): System.out.println(Arrays.toString(list.toArray())); However, as others have pointed out, if you don’t have sensible toString() methods implemented for the objects inside the list, you will get the object pointers (hash codes, in fact) you’re observing. This is true whether they’re … Read more

Java List.add() UnsupportedOperationException

Not every List implementation supports the add() method. One common example is the List returned by Arrays.asList(): it is documented not to support any structural modification (i.e. removing or adding elements) (emphasis mine): Returns a fixed-size list backed by the specified array. Even if that’s not the specific List you’re trying to modify, the answer … Read more

Are tuples more efficient than lists in Python?

Summary Tuples tend to perform better than lists in almost every category: Tuples can be constant folded. Tuples can be reused instead of copied. Tuples are compact and don’t over-allocate. Tuples directly reference their elements. Tuples can be constant folded Tuples of constants can be precomputed by Python’s peephole optimizer or AST-optimizer. Lists, on the … Read more

Difference between List and Array types in Kotlin

Arrays and lists (represented by List<T> and its subtype MutableList<T>) have many differences, here are the most significant ones: Array<T> is a class with known implementation: it’s a sequential fixed-size memory region storing the items (and on JVM it is represented by Java array). List<T> and MutableList<T> are interfaces which have different implementations: ArrayList<T>, LinkedList<T> … Read more

Convert generic List/Enumerable to DataTable?

Here’s a nice 2013 update using FastMember from NuGet: IEnumerable<SomeType> data = … DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data)) { table.Load(reader); } This uses FastMember’s meta-programming API for maximum performance. If you want to restrict it to particular members (or enforce the order), then you can do that too: IEnumerable<SomeType> data = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)