tags:

views:

612

answers:

9

What is this called in python:

[('/', MainPage)]

Is that an array .. of ... erhm one dictionary?

Is that

()

A tuple? ( or whatever they call it? )

+13  A: 

Its a list with a single tuple.

Shaun
and to go even deeper: a tuple with two values.
T Pops
or two elements, yes. Thanks for clarifying. I do not mean to imply (if you took it that way) that this is a single-element tuple. Only that this list contains one tuple.
Shaun
Of 2 elements, one is a string of one caracter. Somebody goes for the encoding ?
e-satis
A single list with a single tuple with a pair of elements with single values!
Carson Myers
@Carson, my head hurts... @.@
David Thomas
+1  A: 

Yes, it's a tuple.

They look like this:

()
(foo,)
(foo, bar)
(foo, bar, baz)

etc.

Eevee
+2  A: 

That's a list of tuples.

This is a list of integers: [1, 2, 3, 4, 5]

This is also a list of integers: [1]

This is a (string, integer) tuple: ("hello world", 42)

This is a list of (string, integer) tuples: [("a", 1), ("b", 2), ("c", 3)]

And so is this: [("a", 1)]

In Python, there's not much difference between lists and tuples. However, they are conceptually different. An easy way to think of it is that a list contains lots of items of the same type (homogeneous) , and a tuple contains a fixed number of items of different types (heterogeneous). An easy way to remember this is that lists can be appended to, and tuples cannot, because appending to a list makes sense and appending to a tuple doesn't.

Python doesn't enforce these distinctions -- in Python, you can append to a tuple with +, or store heterogeneous types in a list.

John Millikin
The paragraph on the difference between lists and tuples has multiple errors. List can have inhomogeneous types, as well as tuples, and tuples are immutable, not just of fixed length, etc.
tom10
@top10: I'm speaking of the conceptual differences between a list and tuple in any language, not just Python. In statically-typed languages such as Java, C++, Haskell, etc, lists are of a single type and tuples are (possibly mutable) fixed-length records.
John Millikin
You cant append to a tuple in Python. They are immutable.
truppo
Sure you can: `(1,2) + (3,4) == (1,2,3,4)`
John Millikin
@John: You're not appending there, you're creating a new tuple. append is a method of mutable sequences, tuples are immutable. Maybe it's a matter of semantics but I think words are important.
Don O'Donnell
Appending doesn't imply that the original value was mutated; for example, Haskell's lists / strings / etc are immutable, but support being appended to eachother.
John Millikin
+1  A: 
[('/', MainPage)]

That's a list consisting of a two element tuple.

()

That's a zero element tuple.

Ryan
+2  A: 

Note that this:

("is not a tuple")

A tuple is defined by the commas, except in the case of the zero-length tuple. This:

"is a tuple",

because of the comma at the end. The parentheses just enforce grouping (again, except in the case of a zero-length tuple.

jcdyer
In general, though, you should almost always put your tuples in parentheses, for consistency and readability. The only exception I make in my own code is when doing tuple unpacking in assignment statements: `x, y = "Alpha Omega".split()`
jcdyer
+1  A: 

It is a list of tuple(s). You can verify that by

x=[('/', MainPage)]
print type(x) # You will find a <list> type here
print type(x[0]) # You will find a <tuple> type here

You can build a dictionary from this type of structure (may be more tuple inside the list) with this code

my_dict = dict(x) # x=[('/',MainPage)]
mshsayem
A: 

It is a list of tuples containing one tuple.

A tuple is just like a list except that it is immutable, meaning that it can't be changed once it's created. You can't add, remove, or change elements in a tuple. If you want your tuple to be different, you have to create a new tuple with the new data. This may sound like a pain but in reality tuples have many benefits both in code safety and speed.

Imagist
+5  A: 

Since no one has answered this bit yet:

A tuple? ( or whatever they call it? )

The word "tuple" comes from maths. In maths, we might talk about (ordered) pairs, if we're doing 2d geometry. Moving to three dimensions means we need triples. In higher dimensions, we need quadruples, quintuples, and, uh, whatever the prefix is for six, and so on. This starts to get to be a pain, and mathematicians also love generalising ("let's work in n dimensions today!"), so they started using the term "n-tuple" for an ordered list of n things (usually numbers).

After that, a bit of natural laziness is all you need to drop the "n-" and we end up with tuples.

John Fouhy
Thanks for the background. Shaun point it out in the comments, but this answer nicely complements the concept ( for Wikipedia answer was too "correct" for me ) +1
OscarRyz
A: 

It's a list of just one tuple. That tuple has two elements, a string and the object MainPage whatever it is.

Both lists and tuples are ordered groups of object, it doesn't matter what kind of object, they can be heterogeneous in both cases.

The main difference between lists and tuples is that tuples are immutable, just like strings.

For example we can define a list and a tuple:

>>> L = ['a', 1, 5, 'b']
>>> T = ('a', 1, 5, 'b')

we can modify elements of L simply by assigning them a new value

>>> print L
['a', 1, 5, 'b']
>>> L[1] = 'c'
>>> print L
['a', 'c', 5, 'b']

This is not true for tuples

>>> print T
('a', 1, 5, 'b')
>>> T[1] = 'c'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

This is because they are immutable. Tuples' elements may be mutable, and you can modify them, for example:

>>> T = (3, ['a', 1, 2], 'lol')
>>> T[1]
['a', 1, 2]
>>> T[1][0] = 'b'
>>> T
(3, ['b', 1, 2], 'lol')

but the list we edited is still the same object, we didn't replaced the tuple's element.

Andrea Ambu