Creating an Array: Difference between revisions

Creating lists from iteratables
(list methods are not related to creating lists)
(Creating lists from iteratables)
Line 358:
 
Tuples are immutable arrays. Note hat tuples are defined by the "," - the parenthesis are optional:
 
empty = ()
numbers = (1, 2, 3, 4, 5)
zeros = (0,) * 10
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]]==
Anonymous user