Building lists

Ian Kelly ian.g.kelly at gmail.com
Mon Oct 20 22:45:19 CEST 2014


On Mon, Oct 20, 2014 at 2:23 PM, Seymore4Head
<Seymore4Head at hotmail.invalid> wrote:
> On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly <ian.g.kelly at gmail.com>
> wrote:
>
>>On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head
>><Seymore4Head at hotmail.invalid> wrote:
>>> For starters I would like to know if you can make a single item list
>>> and then turn it into a 2 item list.  Is there a command for that?
>>
>>You mean like this?
>>
>>>>> the_list = ['first_item']
>>>>> the_list.append('second_item')
>>>>> the_list
>>['first_item', 'second_item']
>
>
> a=(1,2,3)
> b=("Red", "Green", "Blue")
> c=("a"."b,"c")
>
> d=(1,red,a 2,green,b 3,blue,c)
>
> Something like that.

Those are tuples, not lists. Are you trying to create a list of tuples
from a, b, and c? If so, then zip does what you want:

>>> d = list(zip(a, b, c))
>>> d
[(1, 'Red', 'a'), (2, 'Green', 'b'), (3, 'Blue', 'c')]

Or do you want all those elements merged into a single list?

>>> d = list(sum(zip(a, b, c), ()))
>>> d

[1, 'Red', 'a', 2, 'Green', 'b', 3, 'Blue', 'c']



More information about the Python-list mailing list