Apply function to each element of a list

I think you mean to use map instead of filter: Even simpler, you could use str.upper instead of importing from string (thanks to @alecxe): In Python 2.x, map constructs a new list by applying a given function to every element in a list. filter constructs a new list by restricting to elements that evaluate to True with a given function. In Python 3.x, map and filter construct iterators instead of lists, so … Read more

Add list to set?

You can’t add a list to a set because lists are mutable, meaning that you can change the contents of the list after adding it to the set. You can however add tuples to the set, because you cannot change the contents of a tuple: Edit: some explanation: The documentation defines a set as an unordered collection of … Read more

How do I find the duplicates in a list and create another list with them?

To remove duplicates use set(a). To print duplicates, something like: Note that Counter is not particularly efficient (timings) and probably overkill here. set will perform better. This code computes a list of unique elements in the source order: or, more concisely: I don’t recommend the latter style, because it is not obvious what not seen.add(x) is doing (the set add() method always returns None, hence … Read more

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more

Here’s a generator that yields the chunks you want: If you’re using Python 2, you should use xrange() instead of range(): Also you can simply use list comprehension instead of writing a function, though it’s a good idea to encapsulate operations like this in named functions so that your code is easier to understand. Python 3: Python 2 … Read more

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