Creating an Array: Difference between revisions
Content added Content deleted
(list methods are not related to creating lists) |
(Creating lists from iteratables) |
||
Line 358: | Line 358: | ||
Tuples are immutable arrays. Note hat tuples are defined by the "," - the parenthesis are optional: |
Tuples are immutable arrays. Note hat tuples are defined by the "," - the parenthesis are optional: |
||
empty = () |
empty = () |
||
numbers = (1, 2, 3, 4, 5) |
numbers = (1, 2, 3, 4, 5) |
||
zeros = (0,) * 10 |
zeros = (0,) * 10 |
||
anything = (1, 'foo', 2.57, None, zeros) |
anything = (1, 'foo', 2.57, None, zeros) |
||
Both lists and tuples can be created from other iterateables: |
|||
<pre> |
|||
>>> list('abc') |
|||
['a', 'b', 'c'] |
|||
>>> tuple('abc') |
|||
('a', 'b', 'c') |
|||
>>> list({'a': 1, 'b': 2, 'c': 3}) |
|||
['a', 'c', 'b'] |
|||
</pre> |
|||
>>> open('file', 'w').write('1\n2\n3\n') |
|||
>>> list(open('file')) |
|||
['1\n', '2\n', '3\n'] |
|||
==[[Ruby]]== |
==[[Ruby]]== |