List changes unexpectedly after assignment. Why is this and how can I prevent it?

With new_list = my_list, you don’t actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment. To actually copy the list, you have various possibilities: You can use the builtin list.copy() method (available since Python 3.3):new_list = old_list.copy() You can slice it:new_list … Read more

TypeError: list indices must be integers or slices, not list

This is a classic mistake. i in your case is already an element from array (i.e. another list), not an index of array (not an int), so You can check the Python tutorial. Also, try doing this: And see what you get! Also I would advise to stick to naming conventions: variables are normally lower-case (volume, not Volume). In this case i is misleading. row or elem would be much more suitable.

Removing duplicates in lists

The common approach to get a unique collection of items is to use a set. Sets are unordered collections of distinct objects. To create a set from any iterable, you can simply pass it to the built-in set() function. If you later need a real list again, you can similarly pass the set to the list() function. The following example should cover whatever you … Read more

Convert list to array in Java [duplicate]

Either: or: Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way: Update: It is recommended now to use list.toArray(new Foo[0]);, not list.toArray(new Foo[list.size()]);. From JetBrains Intellij Idea inspection: There are two styles to convert a collection to an array: either using a pre-sized array (like … Read more

How to make a flat list out of a list of lists

Given a list of lists t, which means: is faster than the shortcuts posted so far. (t is the list to flatten.) Here is the corresponding function: As evidence, you can use the timeit module in the standard library: Explanation: the shortcuts based on + (including the implied use in sum) are, of necessity, O(T**2) when there are T sublists — as the intermediate … Read more

Finding the index of an item in a list

Reference: Data Structures > More on Lists Caveats follow Note that while this is perhaps the cleanest way to answer the question as asked, index is a rather weak component of the list API, and I can’t remember the last time I used it in anger. It’s been pointed out to me in the comments that because this answer is heavily … Read more

Difference between Arrays.asList(array) and new ArrayList(Arrays.asList(array))

First, let’s see what this does:Arrays.asList(ia) It takes an array ia and creates a wrapper that implements List<Integer>, which makes the original array available as a list. Nothing is copied and all, only a single wrapper object is created. Operations on the list wrapper are propagated to the original array. This means that if you shuffle the list … Read more