How do I remove duplicates from a list, while preserving order?

Here you have some alternatives: http://www.peterbe.com/plog/uniqifiers-benchmark Fastest one: def f7(seq): seen = set() seen_add = seen.add return [x for x in seq if not (x in seen or seen_add(x))] Why assign seen.add to seen_add instead of just calling seen.add? Python is a dynamic language, and resolving seen.add each iteration is more costly than resolving a … Read more

How do I remove the first item from a list?

You can find a short collection of useful list functions here. list.pop(index) >>> l = [‘a’, ‘b’, ‘c’, ‘d’] >>> l.pop(0) ‘a’ >>> l [‘b’, ‘c’, ‘d’] >>> del list[index] >>> l = [‘a’, ‘b’, ‘c’, ‘d’] >>> del l[0] >>> l [‘b’, ‘c’, ‘d’] >>> These both modify your original list. Others have suggested … Read more

Get unique values from a list in python [duplicate]

First declare your list properly, separated by commas. You can get the unique values by converting the list to a set. mylist = [‘nowplaying’, ‘PBS’, ‘PBS’, ‘nowplaying’, ‘job’, ‘debate’, ‘thenandnow’] myset = set(mylist) print(myset) If you use it further as a list, you should convert it back to a list by doing: mynewlist = list(myset) … Read more

What’s the difference between lists and tuples?

Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order. Using this distinction makes code more explicit and understandable. One example would be pairs of page and … Read more

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