Newbie getting confused again

I

It's me

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,
 
J

John Machin

It's me said:
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)]

If by (x) you really mean a tuple with 1 element i.e. (x,) then you
need something like this:
a = (1, 2, 3)
b = [(4,), (5,)]
res = [tuple([x]) for x in a] + b
res [(1,), (2,), (3,), (4,), (5,)]

Otherwise (a simpler requirement):
a = (1, 2, 3)
b = [4, 5]
res = list(a) + b
res [1, 2, 3, 4, 5]

If NEITHER of the above is what you want, you'll need to try being a
bit clearer about what you are trying to achieve.

And a tip: if "a" is really of variable length, you should make it a
list, not a tuple; read the FAQ.

HTH,
John
 
P

Peter Hansen

It's me said:
If I have:

a = (1,2,3)

Note that this is a tuple.
how do I ended up with:

res=[(1), (2), (3), (4), (5)]

Not that this is a list. The two aren't the same thing.
If you don't understand the difference, you might want
to review the tutorial or head over to the tutor list.

Also note that (1) is just the same as 1, so what you
show above is the same as res=[1, 2, 3, 4, 5]. Is that
what you wanted?
without doing:

res=[(a[0]), (a[1]), (a[2]), (4), (5)]

Presumably what you are asking is how you can create
a new list (or tuple?) based on the contents of the
original tuple "a" but with two more elements added?

This will do precisely what you've described, though
it does convert "a" from a tuple into a list because
you couldn't concatenate (join together) the two objects
otherwise:

res = list(a) + [4, 5]


-Peter
 
I

It's me

*bonk, bonk, bonk*

Now I feel better.

Thanks, everybody. The "+" is indeed what I was looking for. It just
didn't occur to me that this is the way you concatenate two lists together.
But of course, that makes sense, doesn't it?

Thanks again.


Peter Hansen said:
It's me said:
If I have:

a = (1,2,3)

Note that this is a tuple.
how do I ended up with:

res=[(1), (2), (3), (4), (5)]

Not that this is a list. The two aren't the same thing.
If you don't understand the difference, you might want
to review the tutorial or head over to the tutor list.

Also note that (1) is just the same as 1, so what you
show above is the same as res=[1, 2, 3, 4, 5]. Is that
what you wanted?
without doing:

res=[(a[0]), (a[1]), (a[2]), (4), (5)]

Presumably what you are asking is how you can create
a new list (or tuple?) based on the contents of the
original tuple "a" but with two more elements added?

This will do precisely what you've described, though
it does convert "a" from a tuple into a list because
you couldn't concatenate (join together) the two objects
otherwise:

res = list(a) + [4, 5]


-Peter
 
M

M.E.Farmer

It's me said:
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
 
I

It's me

Thanks, got it.


M.E.Farmer said:
It's me said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top