python how to create many-to-many of lists inside one list

OZ_

I have variable like this :

message = "hello world"

and I want to put each 2 letters inside one list like this:

list = [['h', 'e'], ['l', 'l'], ...]

I have tried this method:

message = "hello world"
x,test = 0,[[]] * len(message)
for i in message:
   if len(test[x]) >= 2:
      x += 1
      test[x].append(i)
   else:
      test[x].append(i)

but the result was adding hello world for every list.

blorgon

The problem here is that your outer list contains a reference to a single inner list, just repeated. You can see what I mean by taking your resulting test and reassigning the value of one of the elements:

>>> test[1][0] = 9999
[[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
 [9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']]

So even though x is incrementing, you're still just appending to a single list object because your test variable is a list of repeated references to the same object.

You can get around this by using a comprehension to initialize your test variable:

test = [[] for _ in range(len(message))]

You can also use zip and slicing to get what you want in a single line of code:

[[*z] for z in zip(s[0::2], s[1::2])]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

join 2 lists and create one to many relationship

How create many list as array?

How to determine if entries are in a list or not for all existing lists in a many to many relation

How to create virtual List to realize reference like one-to-many without foreign key on "many side"

how to collect many tuple from a list in python in one line?

Concatenation of many lists in Python

Count how many times lists are contained in a list of lists

How to create many http servers into one app?

How to create an object in a one to many relationship

MySQL How to create one to Zero or Many relation

Django - How to create many to one relation

How to create a one to many relationship using GraphQL?

ASP .Net MVC 5 How to create list in Create View? One to many relation in EntityFramework

Counting how many times there are blank lists in a list of list

Is there a way to create one array properties with many mongoDB objectiID inside

List One to Many Relationship

How to get the maximum number, from many lists in a python dictionary, by list index

How to create a one to many relationship based on one to one (three tables)?

creating many lists from list of lists

Entity Framework Core how to list one to many from many to many from a model without using Include().ThenInclude()

Create two lists from one list in python?

comparing two lists but then saying how many times an item in a list matched

ndb many to many, retrieve list of one of the relation

How to do a query using Python list (must have all OR must have at least one element) on a many to many structure with SQLAlchemy?

How to plot many kdeplots on one figure in python

How to make many characteres to be just one in Python?

How to multiply many matrices in one go in python?

How to create One-to-Many and Many-to-One Relationship from one class to another class in Java/IntelliJ

How do I convert a list with one element into a list with many elements in python