Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36864
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-01-15 11:30 -0800 |
| References | <456df1a1-9225-44e7-9aa9-6b6fe3fde4a0@googlegroups.com> <50e66d28$0$30003$c3e8da3$5496439d@news.astraweb.com> |
| Message-ID | <71096f2e-be87-449a-b5d9-7a02ded89a67@googlegroups.com> (permalink) |
| Subject | Re: Question on for loop |
| From | subhabangalore@gmail.com |
On Friday, January 4, 2013 11:18:24 AM UTC+5:30, Steven D'Aprano wrote: > On Thu, 03 Jan 2013 12:04:03 -0800, subhabangalore wrote: > > > > > Dear Group, > > > If I take a list like the following: > > > > > > fruits = ['banana', 'apple', 'mango'] > > > for fruit in fruits: > > > print 'Current fruit :', fruit > > > > > > Now, > > > if I want variables like var1,var2,var3 be assigned to them, we may > > > take, var1=banana, > > > var2=apple, > > > var3=mango > > > > > > but can we do something to assign the variables dynamically > > > > Easy as falling off a log. You can't write "var1", "var2" etc. but you > > can write it as "var[0]", "var[1]" etc. > > > > var = ['banana', 'apple', 'mango'] > > print var[0] # prints 'banana' > > print var[1] # prints 'apple' > > print var[2] # prints 'mango' > > > > > > > > Of course "var" is not a very good variable name. "fruit" or "fruits" > > would be better. > > > > > > > > > > -- > > Steven Actually in many cases it is easy if you get the variable of list value, I was trying something like, def func1(n): list1=["x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"] blnk=[] for i in range(len(list1)): num1="var"+str(i)+"="+list1[i] blnk.append(num1) print blnk Regards, Subhabrata.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Question on for loop subhabangalore@gmail.com - 2013-01-03 12:04 -0800
Re: Question on for loop MRAB <python@mrabarnett.plus.com> - 2013-01-03 20:21 +0000
Re: Question on for loop Peter Otten <__peter__@web.de> - 2013-01-03 21:22 +0100
Re: Question on for loop Matt Jones <matt.walker.jones@gmail.com> - 2013-01-03 14:28 -0600
Re: Question on for loop Don Ross <donrosszx@gmail.com> - 2013-01-03 15:22 -0800
Re: Question on for loop alex23 <wuwei23@gmail.com> - 2013-01-03 16:47 -0800
Re: Question on for loop Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-04 05:48 +0000
Re: Question on for loop subhabangalore@gmail.com - 2013-01-15 11:30 -0800
Re: Question on for loop Alister <alister.ware@ntlworld.com> - 2013-01-04 10:16 +0000
csiph-web