473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Initializing a list of lists

I want to create a list of lists, each of which is identical, but which
can be modified independently i.e:
x = [ [0], [0], [0] ]
x[0].append(1)
x [[0, 1], [0], [0]]

The above construct works if I have only few items, but if I have many,
I'd prefer to write N =3
x =N*[[0]]
x [[0], [0], [0]]

If I now try extending the lists indepently, I cannot, as they all
point to the same list object x[0].append(1)
x

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

Is there a simple way to create a list of independent lists?

Thanks in advance

Thomas Philips

Mar 19 '06 #1
3 1901
ZeD
Ciao, tk****@hotmail.com! Che stavi dicendo?
Is there a simple way to create a list of independent lists?


N=3
x=[[0] for e in range(N)]

--
Up da 1 giorno, 3 ore, 43 minuti e 10 secondi

Mar 19 '06 #2
> The above construct works if I have only few items, but if I have many,
I'd prefer to write
N =3
x =N*[[0]]
x
[[0], [0], [0]]

If I now try extending the lists indepently, I cannot, as they all
point to the same list object
x[0].append(1)
x
[[0, 1], [0, 1], [0, 1]]

Is there a simple way to create a list of independent lists?


My first thought would be
N = 10
x = [[0] for _ in range(N)]
x[0].append(1)
x

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

HTH,

-tkc

Mar 19 '06 #3
tk****@hotmail.com wrote:
I want to create a list of lists, each of which is identical, but which
can be modified independently i.e:
x = [ [0], [0], [0] ]
x[0].append(1)
x [[0, 1], [0], [0]]

The above construct works if I have only few items, but if I have many,
I'd prefer to write N =3
x =N*[[0]]
x [[0], [0], [0]]

If I now try extending the lists indepently, I cannot, as they all
point to the same list object x[0].append(1)
x

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

Is there a simple way to create a list of independent lists?

Try this...

x, y, value = 3, 3, 0
L = [[value]*x for i in xrange(y)]
Cheers,
Ron




Mar 19 '06 #4

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

Similar topics

1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
14
by: KraftDiner | last post by:
I need a matrix of 256 x 256 unsigned short values... The matrix can be implemented as a list of lists or a tuple... So for example: , , ] or ((1,2,3),(4,5,6), (7,8,9)) This is what I have so...
2
by: Cox | last post by:
Hello: My address jsmith435@cox.net is subscribed to at least the PHP General mailing list. I have for days now been trying to unsubscribe from all PHP mail lists. I have followed the...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.