About 106,000 results
Open links in new tab
  1. python - What's the difference between lists and tuples ... - Stack ...

    Dec 9, 2024 · 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 ...

  2. Convert list to tuple in Python - Stack Overflow

    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

  3. python - List vs tuple, when to use each? - Stack Overflow

    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.

  4. python - Find an element in a list of tuples - Stack Overflow

    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).

  5. python - How to convert list of tuples to multiple lists ... - Stack ...

    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.

  6. python - How to convert a list to a list of tuples? - Stack Overflow

    I am newbie to Python and need to convert a list to dictionary. I know that we can convert a list of tuples to a dictionary. This is the input list: L = [1,term1, 3, term2, x, term3,... z, termN] and I want to convert this list to a list of tuples (or directly to a dictionary) like this: [(1, term1), (3, term2), (x, term3), ...(z, termN)]

  7. python - Convert tuple to list and back - Stack Overflow

    Apr 30, 2013 · Since Python 3.5 (PEP 448 -- Additional Unpacking Generalizations) one can use the following literal syntax to convert a tuple to a list: >>> t = (1,2,3) >>> lst = [*t] >>> lst [1, 2, 3] >>> *lst, # back to tuple (1, 2, 3) A list comprehension can be use to convert a tuple of tuples to a list of lists: >>> level1 = ( ... (1,1,1,1,1,1), ...

  8. python - Convert list of tuples to list? - Stack Overflow

    Aug 4, 2016 · Convert list of tuples to list Python Hot Network Questions Is it a faux pas for a zbMATH reviewer of a published math paper to immediately solve/publish problems it leaves open?

  9. Type hinting tuples in Python - Stack Overflow

    Nov 28, 2017 · Just a note: if you need a tuple containing a variable number, you should think whether you should use a list instead. Maybe a tuple is still the right choice, but usually a list would be better in this case.

  10. python - How to sort a list/tuple of lists/tuples by the element at a ...

    Below example, we are sorting a list of tuple that holds the info abt time of certain event and actor name. We are sorting this list by time of event occurrence - which is the 0th element of a tuple. Note - s.sort([cmp[, key[, reverse]]]) sorts the items of s in place

Refresh