8

I'm following a couple of Pythone exercises and I'm stumped at this one.

# C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use a custom key= function to extract the last element form each tuple. def sort_last(tuples): # +++your code here+++ return 

What is a Tuple? Do they mean a List of Lists?

3
  • Where are these exercises? What tutorial are you using? Commented Mar 21, 2010 at 20:26
  • @Sergio could you provide a link? Commented Mar 22, 2010 at 11:36
  • May be this one: code.google.com/edu/languages/google-python-class Commented Oct 20, 2010 at 16:24

7 Answers 7

17

The tuple is the simplest of Python's sequence types. You can think about it as an immutable (read-only) list:

>>> t = (1, 2, 3) >>> print t[0] 1 >>> t[0] = 2 TypeError: tuple object does not support item assignment 

Tuples can be turned into new lists by just passing them to list() (like any iterable), and any iterable can be turned into a new tuple by passing it to tuple():

>>> list(t) [1, 2, 3] >>> tuple(["hello", []]) ("hello", []) 

Hope this helps. Also see what the tutorial has to say about tuples.

Sign up to request clarification or add additional context in comments.

Comments

11

1 Comment

answer to the exercise (not related to tuples): import operator; tuples.sort(key=operator.itemgetter(1))
4

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example −

tup1 = ('Dog', 'Cat', 2222, 555555); tup2 = (10, 20, 30, 40, 50 ); tup3 = "a", "b", "c", "d"; 

The empty tuple is written as two parentheses containing nothing −

tup1 = (); 

To write a tuple containing a single value you have to include a comma, even though there is only one value −

tup1 = (45,); 

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.

Comments

3

A tuple and a list is very similar. The main difference (as a user) is that a tuple is immutable (can't be modified)

In your example:

[(2, 2), (1, 3), (3, 4, 5), (1, 7)] 

This is a list of tuples [...] is the list (2,2) is a tuple

Comments

3

The best summary of the differences between lists and tuples I have read is this:

One common summary of these more interesting, if subtle, differences is that tuples are heterogeneous and lists are homogeneous. In other words: Tuples (generally) are sequences of different kinds of stuff, and you deal with the tuple as a coherent unit. Lists (generally) are sequences of the same kind of stuff, and you deal with the items individually.

From: http://news.e-scribe.com/397

1 Comment

In Haskell, list items must be same Type, while tuple is not. But this is not true in Python. In Python, the difference is immutability.
1

Tuples are used to group related variables together. It's often more convenient to use a tuple rather than writing yet another single-use class. Granted, accessing their content by index is more obscure than a named member variable, but it's always possible to use 'tuple unpacking':

def returnTuple(a, b): return (a, b) a, b = returnTuple(1, 2) 

Comments

0

In Python programming, a tuple is similar to a list. The difference between them is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed. data-types-in-python

Comments