473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

One-step multiples list generation?

Hello,

Suppose I have some non-numerical Foo and would like to create a list
of 20 Foo-s. Is there a one-step method (not a loop) of doing so? E.g.,
something like [Foo * 20] (which is obviously not the right way) that
would create [Foo, Foo, Foo, ...,Foo].
I tried looking through the docs, FAQs and help, but couldn't find
anything (the closest is range, but it seems it's only for numerics) - I
very much appreciate your time in answering. Also, please excuse me if I
used some wrong terminology.

Thanks,

Efrat
Jan 3 '06 #1
7 1127
* Efrat Regev <ef*********@yahoo.com> in comp.lang.python:
Suppose I have some non-numerical Foo and would like to create a list
of 20 Foo-s. Is there a one-step method (not a loop) of doing so?


Maybe :

[ Foo ] * 20

or, more verbose,

[ Foo for _ in range(20) ]

?

--
DW
Jan 3 '06 #2
Damien Wyart wrote:
* Efrat Regev <ef*********@yahoo.com> in comp.lang.python:
Suppose I have some non-numerical Foo and would like to create a list
of 20 Foo-s. Is there a one-step method (not a loop) of doing so?

Maybe :

[ Foo ] * 20

or, more verbose,

[ Foo for _ in range(20) ]


If Foo is mutable, keep this in mind:
a = [ [] ]*20 # A list of 20 empty lists
print id(a[0]) == id(a[1]) True a[0].append(1)
print a [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], [1], [1], [1], [1]]
b = [ [] for _ in range(20) ] # A list of 20 empty lists
print id(b[0]) == id(b[1]) False b[0].append(1)
print b

[[1], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
[], [], []]

Jan 3 '06 #3
Rocco Moretti wrote:
Damien Wyart wrote:
* Efrat Regev <ef*********@yahoo.com> in comp.lang.python:
Suppose I have some non-numerical Foo and would like to create a list
of 20 Foo-s. Is there a one-step method (not a loop) of doing so?


Maybe :

[ Foo ] * 20

or, more verbose,

[ Foo for _ in range(20) ]


If Foo is mutable, keep this in mind:
>>> a = [ [] ]*20 # A list of 20 empty lists
>>> print id(a[0]) == id(a[1]) True >>> a[0].append(1)
>>> print a [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], [1], [1], [1], [1]] >>>
>>> b = [ [] for _ in range(20) ] # A list of 20 empty lists
>>> print id(b[0]) == id(b[1]) False >>> b[0].append(1)
>>> print b [[1], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
[], [], []]


Oh, but also:
Foo = []
c = [ Foo for _ in range(20) ] # A list of 20 empty lists
print id(c[0]) == id(c[1]) True c[0].append(1)
print c

[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1],
[1], [1], [1], [1], [1], [1]]

(The difference with the 'b' case is that a new list is created each
"time around", in the other cases, the same list is reused.
Jan 3 '06 #4
Thanks for these important and useful additions, they are very welcome !

In writing my answer I had immutables in mind, but mutables are a bit
more dangerous, here...

--
DW
Jan 3 '06 #5
Damien Wyart wrote:
* Efrat Regev <ef*********@yahoo.com> in comp.lang.python:
Suppose I have some non-numerical Foo and would like to create a list
of 20 Foo-s. Is there a one-step method (not a loop) of doing so?

Maybe :

[ Foo ] * 20

or, more verbose,

[ Foo for _ in range(20) ]

?


Great. Thanks!
Jan 3 '06 #6
On Tue, 03 Jan 2006 19:21:32 +0100, Damien Wyart wrote:
Thanks for these important and useful additions, they are very welcome !

In writing my answer I had immutables in mind, but mutables are a bit
more dangerous, here...


Mutables are easy to deal with using the copy module and list
comprehensions:
foo = []
import copy
multi_foo = [copy.copy(foo) for _ in range(10)]
multi_foo [[], [], [], [], [], [], [], [], [], []] multi_foo[5].append(None)
multi_foo

[[], [], [], [], [], [None], [], [], [], []]

Or use copy.deepcopy if you need to.

--
Steven.

Jan 4 '06 #7
Damien Wyart wrote:
Thanks for these important and useful additions, they are very welcome !

In writing my answer I had immutables in mind, but mutables are a bit
more dangerous, here...


Not *that* much though.

The first construct can't be used, but he can use
[copy.copy(Foo) for _ in range(20)]


And he should be ok.
Jan 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: M. Clift | last post by:
Hi, Just a quick one. I'm trying to call with this, but I keep getting 'unhashable'. I've tried various ' " and nextName = {:,\ 'Rita':],\ 'Sue':],\ 'Mary':}
7
by: rjl444 | last post by:
I created 2 tables with one to one relationship. if I add a record in table A, how does table B record get created? Does SQL do this automatically because it is one to one relationship? or do I...
7
by: Randy Yates | last post by:
I'm a complete newbie to postgres so please look the other way if these questions are really stupid. Is it legitimate to have one database per data file? For organizational and backup purposes,...
22
by: petermichaux | last post by:
Hi, I'm curious about server load and download time if I use one big javascript file or break it into several smaller ones. Which is better? (Please think of this as the first time the scripts...
3
by: nasirmajor | last post by:
dear all, a simple quetion for database experts. can three datatables be updated at the same time (from one page)with one table having one primary key and other two tables are having that primary...
2
by: Sky | last post by:
Hello: I'm trying to make sense of snk files, when to use, under what conditions to regenerate new ones,...can someone take a look if these statemes make sense? And then the final questions at the...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
25
by: toffee | last post by:
Hi all, apologies if this seems like a pretty basic question - but can an element have more than one class? if so, how do you set them ? and how does the browser give priority to a class over...
5
by: Shogun | last post by:
I have an mdb with a *projects* table. I also have a *timecode* table. Projects and timecodes are related by a one-to-one relationship but I do not want to simply add the timecode field to my...
0
by: CatchSandeepVaid | last post by:
We all know that one-to-one associations are non-lazly fetched but during this fetching the filters are not applied. I debugged hibernate code and found that hibernate finally calls...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.