How do I get the last element of a list? Which way is preferred? Answer some_list[-1] is the shortest and most Pythonic. In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)], which gives
Tag: list
How can I convert a dictionary into a list of tuples?
If I have a dictionary like: How can I convert it to this? And how can I convert it to this? Answer For Python 3.6 and later, the order of the list is what you would expect. In Python 2, you don’t need list.
Subtracting 2 lists in Python
Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like Should I use tuples? If none of them defines these operands on these types, can I define it instead? If not, should I create a new vector3 class? Answer If this is something you end up doing frequently,
Accessing the index in ‘for’ loops
How do I access the index while iterating over a sequence with a for loop? Desired output: Answer Use the built-in function enumerate(): It is non-pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable. Check out PEP 279 for more.
Why is it string.join(list) instead of list.join(string)?
This has always confused me. It seems like this would be nicer: Than this: Is there a specific reason it is like this? Answer It’s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the “joiner” must be strings. For example: Using something other than strings will raise the following error: TypeError: sequence item
How do I remove duplicates from a list, while preserving order?
How do I remove duplicates from a list, while preserving order? Using a set to remove duplicates destroys the original order. Is there a built-in or a Pythonic idiom? Answer Here you have some alternatives: http://www.peterbe.com/plog/uniqifiers-benchmark Fastest one: 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
How to iterate over a list in chunks
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don’t have control of the input, or I’d have it passed in as a list of four-element tuples. Currently, I’m iterating over it this way: It looks a lot like “C-think”, though, which makes
How can I make a dictionary (dict) from separate lists of keys and values?
I want to combine these: Into a single dictionary: Answer Like this: Voila :-) The pairwise dict constructor and zip function are awesomely useful.
How do I check if a list is empty?
This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. For example, if passed the following: How do I check to see if a is empty? Answer Using the implicit booleanness of the empty list is quite Pythonic.