Newbie getting confused again

M.E.Farmer mefjr75 at hotmail.com
Sat Mar 5 17:21:48 CET 2005


It's me wrote:
> If I have:
>
>     a = (1,2,3)
>
> how do I ended up with:
>
>     res=[(1), (2), (3), (4), (5)]
>
> without doing:
>
>     res=[(a[0]), (a[1]), (a[2]), (4), (5)]
>
> ???
>
> ps: This is just a nobrainer example of what my real code is trying
to do.
> "a" might have many many elements.   That's why the explicit indexing
method
> won't work.
>
> Thanks,
Hello,
List objects have a method called extend().
It is made for this.
Py> a = [1,2,3]
Py> b = [4,5,6]
Py> a.extend(b)
Py> a
[1, 2, 3, 4, 5, 6]
Since you are a newbie I also suggest you look at
your objects a little and see what they have available.

Py>dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__repr__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__str__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort']

Then you can try and get some help from Python.
Py>help(a.extend)
Help on built-in function extend:

extend(...)
    L.extend(iterable) -- extend list by appending elements from the
iterable

And finally use pydoc it is very helpful.
Cl> python pydoc -g
hth,
M.E.Farmer




More information about the Python-list mailing list