What is the difference between Set and List?

List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered (thank you, Quinn Taylor). List<E>: An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their … Read more

How to initialize List object in Java?

If you check the API for List you’ll notice it says: Interface List<E> Being an interface means it cannot be instantiated (no new List() is possible). If you check that link, you’ll find some classes that implement List: All Known Implementing Classes: AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector Some of those … Read more

Get list from pandas dataframe column or row?

Pandas DataFrame columns are Pandas Series when you pull them out, which you can then call x.tolist() on to turn them into a Python list. Alternatively you cast it with list(x). import pandas as pd data_dict = {‘one’: pd.Series([1, 2, 3], index=[‘a’, ‘b’, ‘c’]), ‘two’: pd.Series([1, 2, 3, 4], index=[‘a’, ‘b’, ‘c’, ‘d’])} df = … Read more

Flatten an irregular (arbitrarily nested) list of lists

Using generator functions can make your example easier to read and improve performance. Python 2 Using the Iterable ABC added in 2.6: from collections import Iterable def flatten(xs): for x in xs: if isinstance(x, Iterable) and not isinstance(x, basestring): for item in flatten(x): yield item else: yield x Python 3 In Python 3, basestring is … Read more

Sort a list of tuples by 2nd item (integer value) [duplicate]

Try using the key keyword with sorted(). sorted([(‘abc’, 121),(‘abc’, 231),(‘abc’, 148), (‘abc’,221)], key=lambda x: x[1]) key should be a function that identifies how to retrieve the comparable element from your data structure. In your case, it is the second element of the tuple, so we access [1]. For optimization, see jamylak’s response using itemgetter(1), which … Read more

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