Search results
Results from the WOW.Com Content Network
So, if you want the first 3 values of a given list or tuple, the syntax would be the same: >>> my_list[0:3] [0,1,2] >>> my_tuple[0:3] [a,b,c] Comparing & Sorting - Two tuples or two lists are both compared by their first element, and if there is a tie, then by the second element, and so on. No further attention is paid to subsequent elements ...
L is a list and we want to convert it to a tuple. L = [1, 2, 3] tuple(L) By invoking tuple, you convert the list (L) into a tuple. As done above. >> (1, 2, 3) you can go ahead and access any item in the tuple using the square brackets. L[0] 1
Specifically, dict(X) converts X into a dictionary where the last tuple of any common first element, is the value that is used. In the example of the OP, this would return (1,4) as opposed to both (1,2) and (1,4).
This is what I normally do in order to ascertain that the input is a list/tuple - but not a str. Because many times I stumbled upon bugs where a function passes a str object by mistake, and the target function does for x in lst assuming that lst is actually a list or tuple. assert isinstance(lst, (list, tuple))
Suppose I have a list of tuples and I want to convert to multiple lists. For example, the list of tuples is [(1,2),(3,4),(5,6),] Is there any built-in function in Python that convert it to: [1,3,5],[2,4,6] This can be a simple program. But I am just curious about the existence of such built-in function in Python.
And I gave a counter example which (1) is a non-evil use of isinstance, because having a fallback means it doesn't break ducktyping, and (2) is a good solution for a very common motivation people have for wanting to check if something is a list or tuple (i.e. to disambiguate them from strings).
For input parameters it's best to accept the most generic interface that does what you need. It is seldom just a tuple or list - more often it's sequence, sliceable or even iterable. Python's duck typing usually gets it for free, unless you explicitly check input types. Don't do that unless absolutely unavoidable.
As mentioned, it is a list comprehension (covered in DiveIntoPython3, for example). Here are a few points: [x[1] for x in L] Notice the []'s around the line of code. These are what define a list. This tells you that this code returns a list, so it's of the list type. Hence, this technique is called a "list comprehension." L is your original list.
Besides len you can also use operator.length_hint (requires Python 3.4+). For a normal list both are equivalent, but length_hint makes it possible to get the length of a list-iterator, which could be useful in certain circumstances:
From my point of view, it seem's what I'm looking for and to work fine for the both (list and tuple). Because when you use print, you will see the right value (as expected and mentionned by @cyborg and @Lodewijk) and nothing related to the object such as : <map object at 0x000001F266DCE5C0> or <zip object at 0x000002629D204C88>.