Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79320 > unrolled thread

Building lists

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-10-20 14:10 -0400
Last post2014-10-20 21:27 -0400
Articles 20 on this page of 32 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 14:10 -0400
    Re: Building lists sohcahtoa82@gmail.com - 2014-10-20 11:18 -0700
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 14:21 -0400
        Re: Building lists sohcahtoa82@gmail.com - 2014-10-20 11:33 -0700
          Re: Building lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-20 20:55 +0100
    Re: Building lists MRAB <python@mrabarnett.plus.com> - 2014-10-20 20:40 +0100
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 15:49 -0400
        Re: Building lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 13:58 -0600
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 16:23 -0400
            Re: Building lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 14:45 -0600
              Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 16:55 -0400
            Re: Building lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-20 21:47 +0100
        Re: Building lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-20 20:57 +0100
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 16:24 -0400
        Re: Building lists Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:40 +0000
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 18:44 -0400
        Re: Building lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-21 10:55 +1100
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 20:05 -0400
        Re: Building lists Larry Hudson <orgnut@yahoo.com> - 2014-10-21 00:11 -0700
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-21 08:54 -0400
        Re: Building lists Larry Hudson <orgnut@yahoo.com> - 2014-10-21 00:40 -0700
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-21 08:55 -0400
      Re: Building lists Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:30 +0000
        Re: Building lists MRAB <python@mrabarnett.plus.com> - 2014-10-20 23:57 +0100
    Re: Building lists John Gordon <gordon@panix.com> - 2014-10-20 20:48 +0000
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 17:25 -0400
        Re: Building lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 15:34 -0600
        Re: Building lists Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-20 21:31 -0400
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-21 08:55 -0400
    Re: Building lists Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-21 10:54 +1300
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 17:57 -0400
    Re: Building lists Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-20 21:27 -0400

Page 1 of 2  [1] 2  Next page →


#79320 — Building lists

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 14:10 -0400
SubjectBuilding lists
Message-ID<obja4a5ij57rtuaivtvcuqsufhuhk3a2su@4ax.com>
I haven't had a lot of practice doing this.  If anyone knows of a site
I would appreciate it.

Will Python work like this:
I am trying to come up with an example and work to it.  Say I want a
Grocery list and it will have a maximum size of 50 items.

I want to do this with one list.
Make a list of 0-50.
Then can I add to that list so the second item will hold something
like cheese, eggs, milk.
Say then I want to add the price of cheese, eggs and milk.
Say then I want to add another list of price of cheese, eggs milk from
another store.

Can this be done starting with just a list of numbers from 0-50?
Please no hints, just answer directly how it is done.

for store in range (50):
	print store

How can I add to store where it looks like this:
(0,cheese, 1,eggs 2,milk , 3-50,blank for now)
	

[toc] | [next] | [standalone]


#79321

Fromsohcahtoa82@gmail.com
Date2014-10-20 11:18 -0700
Message-ID<ebbed5aa-bc89-4034-91db-e85fae4d9b51@googlegroups.com>
In reply to#79320
On Monday, October 20, 2014 11:13:17 AM UTC-7, Seymore4Head wrote:
> I haven't had a lot of practice doing this.  If anyone knows of a site
> 
> I would appreciate it.
> 
> 
> 
> Will Python work like this:
> 
> I am trying to come up with an example and work to it.  Say I want a
> 
> Grocery list and it will have a maximum size of 50 items.
> 
> 
> 
> I want to do this with one list.
> 
> Make a list of 0-50.
> 
> Then can I add to that list so the second item will hold something
> 
> like cheese, eggs, milk.
> 
> Say then I want to add the price of cheese, eggs and milk.
> 
> Say then I want to add another list of price of cheese, eggs milk from
> 
> another store.
> 
> 
> 
> Can this be done starting with just a list of numbers from 0-50?
> 
> Please no hints, just answer directly how it is done.
> 
> 
> 
> for store in range (50):
> 
> 	print store
> 
> 
> 
> How can I add to store where it looks like this:
> 
> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)

"Please no hints, just answer directly how it is done."

That's pretty rude.  We're not going to do your homework for you.

[toc] | [prev] | [next] | [standalone]


#79322

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 14:21 -0400
Message-ID<mjka4ad8lnmlvu0ho75usq3nnpr53kfeh0@4ax.com>
In reply to#79321
On Mon, 20 Oct 2014 11:18:26 -0700 (PDT), sohcahtoa82@gmail.com wrote:

>On Monday, October 20, 2014 11:13:17 AM UTC-7, Seymore4Head wrote:
>> I haven't had a lot of practice doing this.  If anyone knows of a site
>> 
>> I would appreciate it.
>> 
>> 
>> 
>> Will Python work like this:
>> 
>> I am trying to come up with an example and work to it.  Say I want a
>> 
>> Grocery list and it will have a maximum size of 50 items.
>> 
>> 
>> 
>> I want to do this with one list.
>> 
>> Make a list of 0-50.
>> 
>> Then can I add to that list so the second item will hold something
>> 
>> like cheese, eggs, milk.
>> 
>> Say then I want to add the price of cheese, eggs and milk.
>> 
>> Say then I want to add another list of price of cheese, eggs milk from
>> 
>> another store.
>> 
>> 
>> 
>> Can this be done starting with just a list of numbers from 0-50?
>> 
>> Please no hints, just answer directly how it is done.
>> 
>> 
>> 
>> for store in range (50):
>> 
>> 	print store
>> 
>> 
>> 
>> How can I add to store where it looks like this:
>> 
>> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)
>
>"Please no hints, just answer directly how it is done."
>
>That's pretty rude.  We're not going to do your homework for you.

I said please

[toc] | [prev] | [next] | [standalone]


#79323

Fromsohcahtoa82@gmail.com
Date2014-10-20 11:33 -0700
Message-ID<677ff292-256a-41e2-9f30-6fa5ff2ce928@googlegroups.com>
In reply to#79322
On Monday, October 20, 2014 11:23:36 AM UTC-7, Seymore4Head wrote:
> On Mon, 20 Oct 2014 11:18:26 -0700 (PDT), sohcahtoa82 wrote:
> 
> 
> 
> >On Monday, October 20, 2014 11:13:17 AM UTC-7, Seymore4Head wrote:
> 
> >> I haven't had a lot of practice doing this.  If anyone knows of a site
> 
> >> 
> 
> >> I would appreciate it.
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> Will Python work like this:
> 
> >> 
> 
> >> I am trying to come up with an example and work to it.  Say I want a
> 
> >> 
> 
> >> Grocery list and it will have a maximum size of 50 items.
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> I want to do this with one list.
> 
> >> 
> 
> >> Make a list of 0-50.
> 
> >> 
> 
> >> Then can I add to that list so the second item will hold something
> 
> >> 
> 
> >> like cheese, eggs, milk.
> 
> >> 
> 
> >> Say then I want to add the price of cheese, eggs and milk.
> 
> >> 
> 
> >> Say then I want to add another list of price of cheese, eggs milk from
> 
> >> 
> 
> >> another store.
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> Can this be done starting with just a list of numbers from 0-50?
> 
> >> 
> 
> >> Please no hints, just answer directly how it is done.
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> for store in range (50):
> 
> >> 
> 
> >> 	print store
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> How can I add to store where it looks like this:
> 
> >> 
> 
> >> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)
> 
> >
> 
> >"Please no hints, just answer directly how it is done."
> 
> >
> 
> >That's pretty rude.  We're not going to do your homework for you.
> 
> 
> 
> I said please

Oh shit!  It's the magic word!  I better get right on it!

[toc] | [prev] | [next] | [standalone]


#79329

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-10-20 20:55 +0100
Message-ID<mailman.15004.1413834962.18130.python-list@python.org>
In reply to#79323
On 20/10/2014 19:33, sohcahtoa82@gmail.com wrote:
>
> Oh shit!  It's the magic word!  I better get right on it!
>

While you're at it would you please access this list via 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#79325

FromMRAB <python@mrabarnett.plus.com>
Date2014-10-20 20:40 +0100
Message-ID<mailman.15001.1413834029.18130.python-list@python.org>
In reply to#79320
On 2014-10-20 19:10, Seymore4Head wrote:
> I haven't had a lot of practice doing this.  If anyone knows of a site
> I would appreciate it.
>
> Will Python work like this:
> I am trying to come up with an example and work to it.  Say I want a
> Grocery list and it will have a maximum size of 50 items.
>
> I want to do this with one list.
> Make a list of 0-50.
> Then can I add to that list so the second item will hold something
> like cheese, eggs, milk.
> Say then I want to add the price of cheese, eggs and milk.
> Say then I want to add another list of price of cheese, eggs milk from
> another store.
>
> Can this be done starting with just a list of numbers from 0-50?
> Please no hints, just answer directly how it is done.
>
> for store in range (50):
> 	print store
>
> How can I add to store where it looks like this:
> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)
> 	
The grocery list would be a list of the things you want to buy.

There are a number of stores, so that would be a list of stores. For
each store you want the price of each item, so that would be a dict
where the key is the item and the value is the price of that item. That
means it would be a list of dicts.

Does that help?

[toc] | [prev] | [next] | [standalone]


#79328

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 15:49 -0400
Message-ID<6jpa4ahhc8ln7hk8s0258hvqd3io51epa0@4ax.com>
In reply to#79325
On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
wrote:

>On 2014-10-20 19:10, Seymore4Head wrote:
>> I haven't had a lot of practice doing this.  If anyone knows of a site
>> I would appreciate it.
>>
>> Will Python work like this:
>> I am trying to come up with an example and work to it.  Say I want a
>> Grocery list and it will have a maximum size of 50 items.
>>
>> I want to do this with one list.
>> Make a list of 0-50.
>> Then can I add to that list so the second item will hold something
>> like cheese, eggs, milk.
>> Say then I want to add the price of cheese, eggs and milk.
>> Say then I want to add another list of price of cheese, eggs milk from
>> another store.
>>
>> Can this be done starting with just a list of numbers from 0-50?
>> Please no hints, just answer directly how it is done.
>>
>> for store in range (50):
>> 	print store
>>
>> How can I add to store where it looks like this:
>> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)
>> 	
>The grocery list would be a list of the things you want to buy.
>
>There are a number of stores, so that would be a list of stores. For
>each store you want the price of each item, so that would be a dict
>where the key is the item and the value is the price of that item. That
>means it would be a list of dicts.
>
>Does that help?

No.  I am pretty new to Python.

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?

Do you have to know the number of items the list will have before
making it?

[toc] | [prev] | [next] | [standalone]


#79330

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-20 13:58 -0600
Message-ID<mailman.15005.1413835170.18130.python-list@python.org>
In reply to#79328
On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head
<Seymore4Head@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']

[toc] | [prev] | [next] | [standalone]


#79332

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 16:23 -0400
Message-ID<g9ra4adb83v03vjarh5vlhgpff0mhkilt7@4ax.com>
In reply to#79330
On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly <ian.g.kelly@gmail.com>
wrote:

>On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head
><Seymore4Head@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.

[toc] | [prev] | [next] | [standalone]


#79335

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-20 14:45 -0600
Message-ID<mailman.15007.1413837963.18130.python-list@python.org>
In reply to#79332
On Mon, Oct 20, 2014 at 2:23 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly <ian.g.kelly@gmail.com>
> wrote:
>
>>On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head
>><Seymore4Head@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']

[toc] | [prev] | [next] | [standalone]


#79338

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 16:55 -0400
Message-ID<03ta4ahogsj145ijd7lfshm7emh6hh52gh@4ax.com>
In reply to#79335
On Mon, 20 Oct 2014 14:45:19 -0600, Ian Kelly <ian.g.kelly@gmail.com>
wrote:

>On Mon, Oct 20, 2014 at 2:23 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly <ian.g.kelly@gmail.com>
>> wrote:
>>
>>>On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head
>>><Seymore4Head@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']

I am not sure really.  I know I need more practice, but I haven't
found a reliable way to find practice problems.

Thank you

[toc] | [prev] | [next] | [standalone]


#79336

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-10-20 21:47 +0100
Message-ID<mailman.15008.1413838079.18130.python-list@python.org>
In reply to#79332
On 20/10/2014 21:23, Seymore4Head wrote:
> On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly <ian.g.kelly@gmail.com>
> wrote:
>
>> On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head
>> <Seymore4Head@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.
>

https://docs.python.org/3/library/functions.html#zip

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#79331

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-10-20 20:57 +0100
Message-ID<mailman.15006.1413835207.18130.python-list@python.org>
In reply to#79328
On 20/10/2014 20:49, Seymore4Head wrote:
> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
> wrote:
>
>> On 2014-10-20 19:10, Seymore4Head wrote:
>>> I haven't had a lot of practice doing this.  If anyone knows of a site
>>> I would appreciate it.
>>>
>>> Will Python work like this:
>>> I am trying to come up with an example and work to it.  Say I want a
>>> Grocery list and it will have a maximum size of 50 items.
>>>
>>> I want to do this with one list.
>>> Make a list of 0-50.
>>> Then can I add to that list so the second item will hold something
>>> like cheese, eggs, milk.
>>> Say then I want to add the price of cheese, eggs and milk.
>>> Say then I want to add another list of price of cheese, eggs milk from
>>> another store.
>>>
>>> Can this be done starting with just a list of numbers from 0-50?
>>> Please no hints, just answer directly how it is done.
>>>
>>> for store in range (50):
>>> 	print store
>>>
>>> How can I add to store where it looks like this:
>>> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)
>>> 	
>> The grocery list would be a list of the things you want to buy.
>>
>> There are a number of stores, so that would be a list of stores. For
>> each store you want the price of each item, so that would be a dict
>> where the key is the item and the value is the price of that item. That
>> means it would be a list of dicts.
>>
>> Does that help?
>
> No.  I am pretty new to Python.
>
> 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?
>
> Do you have to know the number of items the list will have before
> making it?
>

Python doesn't have commands and no.  Perhaps you'd care to (re)read the 
tutorial.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#79333

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 16:24 -0400
Message-ID<dqra4alvtrb72n1bb2bc66beicin2s90od@4ax.com>
In reply to#79331
On Mon, 20 Oct 2014 20:57:52 +0100, Mark Lawrence
<breamoreboy@yahoo.co.uk> wrote:

>On 20/10/2014 20:49, Seymore4Head wrote:
>> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
>> wrote:
>>
>>> On 2014-10-20 19:10, Seymore4Head wrote:
>>>> I haven't had a lot of practice doing this.  If anyone knows of a site
>>>> I would appreciate it.
>>>>
>>>> Will Python work like this:
>>>> I am trying to come up with an example and work to it.  Say I want a
>>>> Grocery list and it will have a maximum size of 50 items.
>>>>
>>>> I want to do this with one list.
>>>> Make a list of 0-50.
>>>> Then can I add to that list so the second item will hold something
>>>> like cheese, eggs, milk.
>>>> Say then I want to add the price of cheese, eggs and milk.
>>>> Say then I want to add another list of price of cheese, eggs milk from
>>>> another store.
>>>>
>>>> Can this be done starting with just a list of numbers from 0-50?
>>>> Please no hints, just answer directly how it is done.
>>>>
>>>> for store in range (50):
>>>> 	print store
>>>>
>>>> How can I add to store where it looks like this:
>>>> (0,cheese, 1,eggs 2,milk , 3-50,blank for now)
>>>> 	
>>> The grocery list would be a list of the things you want to buy.
>>>
>>> There are a number of stores, so that would be a list of stores. For
>>> each store you want the price of each item, so that would be a dict
>>> where the key is the item and the value is the price of that item. That
>>> means it would be a list of dicts.
>>>
>>> Does that help?
>>
>> No.  I am pretty new to Python.
>>
>> 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?
>>
>> Do you have to know the number of items the list will have before
>> making it?
>>
>
>Python doesn't have commands and no.  Perhaps you'd care to (re)read the 
>tutorial.

Thank you

[toc] | [prev] | [next] | [standalone]


#79346

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-20 22:40 +0000
Message-ID<m2431i$mdn$3@dont-email.me>
In reply to#79328
On Mon, 20 Oct 2014 15:49:15 -0400, Seymore4Head 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?

Yes, it's called assignment. You can for example change a member of a 
list from an string to a tuple such as ( string, number ):

>>> x = [ "fred", "jim", "susan" ]
>>> x[x.index("jim")] = ( "jim", 11, )
>>> print x
['fred', ('jim', 11), 'susan']

> Do you have to know the number of items the list will have before making
> it?

No. See the append() method of the list object.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#79347

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 18:44 -0400
Message-ID<e24b4ahvp9j188dq33bg06f26otpb7s0h1@4ax.com>
In reply to#79346
On Mon, 20 Oct 2014 22:40:50 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> wrote:

>On Mon, 20 Oct 2014 15:49:15 -0400, Seymore4Head 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?
>
>Yes, it's called assignment. You can for example change a member of a 
>list from an string to a tuple such as ( string, number ):
>
>>>> x = [ "fred", "jim", "susan" ]
>>>> x[x.index("jim")] = ( "jim", 11, )
>>>> print x
>['fred', ('jim', 11), 'susan']
>
>> Do you have to know the number of items the list will have before making
>> it?
>
>No. See the append() method of the list object.

Thanks

[toc] | [prev] | [next] | [standalone]


#79356

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-10-21 10:55 +1100
Message-ID<5445a0de$0$12981$c3e8da3$5496439d@news.astraweb.com>
In reply to#79328
Seymore4Head 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?
> 
> Do you have to know the number of items the list will have before
> making it?

Now these are the right sort of questions that you should be asking!

They are concrete and tightly focused, not vague and so broad that they
could mean anything, and they invite a concrete answer rather than hints.

First question: can you take a single list item and turn it into a 2-item
list? Yes, you can, you can turn a single list item into *anything*,
including deleting it. Start with a list, and remember that lists are
indexed starting with 0:

py> alist = [1, 2, 4, 8, 16, 32, 64]
py> alist[0] = "hello!"
py> alist[1] = ["a", "b", "c"]
py> del alist[4]
py> print(alist)
['hello!', ['a', 'b', 'c'], 4, 8, 32, 64]


Second question: do you need to know the number of items in a list in
advance? No. You can add additional items to an existing list with the
append() method:

py> import random
py> alist = []
py> while random.random() < 0.5:
...     alist.append("spam")
...
py> print(alist)
['spam', 'spam']


Because this is random, if you try it you will get an unpredictable number
of "spam" words in the list. About half the time it will contain no words
at all, about a quarter of the time it will contain a single word, an
eighth of the time it will contain two words, and so forth.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#79357

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 20:05 -0400
Message-ID<ro8b4adff4g13n86g83i10uupu9gd3g92a@4ax.com>
In reply to#79356
On Tue, 21 Oct 2014 10:55:08 +1100, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:

>Seymore4Head 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?
>> 
>> Do you have to know the number of items the list will have before
>> making it?
>
>Now these are the right sort of questions that you should be asking!
>
>They are concrete and tightly focused, not vague and so broad that they
>could mean anything, and they invite a concrete answer rather than hints.
>
>First question: can you take a single list item and turn it into a 2-item
>list? Yes, you can, you can turn a single list item into *anything*,
>including deleting it. Start with a list, and remember that lists are
>indexed starting with 0:
>
>py> alist = [1, 2, 4, 8, 16, 32, 64]
>py> alist[0] = "hello!"
>py> alist[1] = ["a", "b", "c"]
>py> del alist[4]
>py> print(alist)
>['hello!', ['a', 'b', 'c'], 4, 8, 32, 64]
>
>
>Second question: do you need to know the number of items in a list in
>advance? No. You can add additional items to an existing list with the
>append() method:
>
>py> import random
>py> alist = []
>py> while random.random() < 0.5:
>...     alist.append("spam")
>...
>py> print(alist)
>['spam', 'spam']
>
>
>Because this is random, if you try it you will get an unpredictable number
>of "spam" words in the list. About half the time it will contain no words
>at all, about a quarter of the time it will contain a single word, an
>eighth of the time it will contain two words, and so forth.

Thanks

[toc] | [prev] | [next] | [standalone]


#79371

FromLarry Hudson <orgnut@yahoo.com>
Date2014-10-21 00:11 -0700
Message-ID<U4ednbgW0r43mtvJnZ2dnUU7-TWdnZ2d@giganews.com>
In reply to#79328
On 10/20/2014 12:49 PM, Seymore4Head wrote:
> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
> wrote:

<snip>

> Do you have to know the number of items the list will have before
> making it?
>

No, it is not necessary, lists are NOT the same as arrays in other languages.  But it IS 
possible to create an initial list of a specific size:

myList = [None] * 50

That creates a 50-element list with each element set to None.  (BTW, the indexes are from 0-49, 
not 0-50.)  I have found this occasionally useful, but I'll emphasize, it's only RARELY useful. 
  The .append() method is far more versatile.

As to your original problem:  my question to you is what is your purpose?

1)  To solve this particular problem, using Python.
   or
2)  To explore the usage of lists, applying them to this problem.

If your purpose is the first, then I agree with the advice you have already been given here. 
Dictionaries are a much better fit to this problem.

If your purpose is the second, then go ahead and use this for your exploration.  But realize 
that to more experienced Pythonistas this would be a very un-pythonic approach.  Even better 
would be to try multiple approaches -- lists, dictionaries, lists with dictionaries, 
dictionaries with lists or tuples...  And any other combinations you can come up with.  This 
will give you even more experience, and allow you to evaluate the different approaches.

And no, I will not give you a ready-made "canned" answer.  For one thing, your description is 
too vague to effectively do that.  Good luck.

      -=- Larry -=-

[toc] | [prev] | [next] | [standalone]


#79377

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-21 08:54 -0400
Message-ID<mklc4at3nusn0hglos7evpr5jnpe23iqqh@4ax.com>
In reply to#79371
On Tue, 21 Oct 2014 00:11:38 -0700, Larry Hudson <orgnut@yahoo.com>
wrote:

>On 10/20/2014 12:49 PM, Seymore4Head wrote:
>> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
>> wrote:
>
><snip>
>
>> Do you have to know the number of items the list will have before
>> making it?
>>
>
>No, it is not necessary, lists are NOT the same as arrays in other languages.  But it IS 
>possible to create an initial list of a specific size:
>
>myList = [None] * 50
>
>That creates a 50-element list with each element set to None.  (BTW, the indexes are from 0-49, 
>not 0-50.)  I have found this occasionally useful, but I'll emphasize, it's only RARELY useful. 
>  The .append() method is far more versatile.
>
>As to your original problem:  my question to you is what is your purpose?
>
>1)  To solve this particular problem, using Python.
>   or
>2)  To explore the usage of lists, applying them to this problem.
>
>If your purpose is the first, then I agree with the advice you have already been given here. 
>Dictionaries are a much better fit to this problem.
>
>If your purpose is the second, then go ahead and use this for your exploration.  But realize 
>that to more experienced Pythonistas this would be a very un-pythonic approach.  Even better 
>would be to try multiple approaches -- lists, dictionaries, lists with dictionaries, 
>dictionaries with lists or tuples...  And any other combinations you can come up with.  This 
>will give you even more experience, and allow you to evaluate the different approaches.
>
>And no, I will not give you a ready-made "canned" answer.  For one thing, your description is 
>too vague to effectively do that.  Good luck.
>
>      -=- Larry -=-

The concept I was asking about was a master list with my example of
1,2,3 as a index for the second and third items.  It was suggested to
make my task easier.  It turns out that it didn't. 
Thanks for all the suggestions, though.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web