##### Add items from one list to list of lists
 Add items from one list to list of lists PUP280 Programmer named Tim Posts: 10 Threads: 3 Joined: Mar 2020 Reputation: 0 Mar-24-2020, 10:50 AM (This post was last modified: Mar-24-2020, 11:07 AM by buran.) Hello, I would like to add every element of a list on every list from a list of lists. Ie : ```H = [[1,2],[3,4]] I = [5,6]```With this result : ``Output:J = [[1,2,5], [3,4,5], [1,2,6], [3,4,6]]``I don’t find the good logic. Ie : ```j = [] for x in h : for y in i : j.append(h+i) print (j)```Gives me : ``````Output:[[[1, 2], [3, 4], 5, 6]] [[[1, 2], [3, 4], 5, 6], [[1, 2], [3, 4], 5, 6]] [[[1, 2], [3, 4], 5, 6], [[1, 2], [3, 4], 5, 6], [[1, 2], [3, 4], 5, 6]] [[[1, 2], [3, 4], 5, 6], [[1, 2], [3, 4], 5, 6], [[1, 2], [3, 4], 5, 6], [[1, 2], [3, 4], 5, 6]]``````Or again : ```k = [] for w in h : for z in i : o = [w,z] k.append(o) print (k)```Will give : ``````Output:[[[1, 2], 5]] [[[1, 2], 5], [[1, 2], 6]] [[[1, 2], 5], [[1, 2], 6], [[3, 4], 5]] [[[1, 2], 5], [[1, 2], 6], [[3, 4], 5], [[3, 4], 6]]``````I’ve tried some tests with map, but nothing conclusive. Can you help me please to solve my problem ? Reply perfringo Da Bishop Posts: 1,567 Threads: 7 Joined: Jun 2018 Reputation: 152 Mar-24-2020, 12:36 PM (This post was last modified: Mar-24-2020, 12:36 PM by perfringo.) My subjective opinion is that 'not finding good logic' starts with bad naming habits. What are H and I? The last name ('I') I would call 'crime against readers of your code', PEP8 Names to avoid: Quote:Never use the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names. In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use 'l', use 'L' instead. I add that 'l' and 'I' are also pretty similar to each other. Now to problem at hand: list comprehension could be suitable tool: ```>>> target = [[1,2],[3,4]] >>> source = [5,6] >>> [[*item, num] for num in source for item in target] [[1, 2, 5], [3, 4, 5], [1, 2, 6], [3, 4, 6]] ``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply PUP280 Programmer named Tim Posts: 10 Threads: 3 Joined: Mar 2020 Reputation: 0 Mar-28-2020, 08:34 AM (This post was last modified: Mar-28-2020, 08:34 AM by PUP280.) Hello, At first, thank you for the edit from the moderator, I had not found how to present it, now I know. Thanks again. For the letters, thank you. I'm a beginner, I knew that but as it was just an example I've believed it was ok. Sorry, I've noticed and I'll don't do that again. Now, about the solution, I thank you. Can you give me an explanation with it please ? I search to understand everything. For what I see, you've added a star in front of item ; is that the way to do what I want ? Have you a link where it's teaching and can you write the long version to help my comprehension ? On Internet, I can't find an explanation (I've read all the tutorial section on Python documentation, maybe I've jumped something). Item isn't a method which gives a dictionnary ? Here item is just a word (I've tried your code changing item to be sure), but it a function too no ? Sorry for my lack of knowledge. Reply perfringo Da Bishop Posts: 1,567 Threads: 7 Joined: Jun 2018 Reputation: 152 May-02-2020, 12:37 PM (This post was last modified: May-02-2020, 12:38 PM by perfringo.) (Mar-28-2020, 08:34 AM)PUP280 Wrote: Can you give me an explanation with it please ? This is nested list comprehension combined with iterable unpacking. Documentation > The Python Tutorial > Data Structures > 5. More on Lists > 5.1.3. List Comprehensions Documentation > The Python Language Reference > 6. Expressions > 6.15. Expression Lists PEP448 - Additional Unpacking Generalizations I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply pyzyx3qwerty Minister of Silly Walks Posts: 356 Threads: 13 Joined: Mar 2020 Reputation: 9 May-02-2020, 01:50 PM (Mar-24-2020, 10:50 AM)PUP280 Wrote: Or again : ```k = [] for w in h : for z in i : o = [w,z] k.append(o) print (k)```Will give : ``````Output:[[[1, 2], 5]] [[[1, 2], 5], [[1, 2], 6]] [[[1, 2], 5], [[1, 2], 6], [[3, 4], 5]] [[[1, 2], 5], [[1, 2], 6], [[3, 4], 5], [[3, 4], 6]]``````I’ve tried some tests with map, but nothing conclusive. Can you help me please to solve my problem ?Had you realized, the code is actually right just that the print statement is in the wrong place. It should be : ```H = [[1,2],[3,4]] I = [5,6] k = [] for w in H : for z in I : o = [w,z] k.append(o) print (k) ```And it gives output: ``````Output: [[[1, 2], 5], [[1, 2], 6], [[3, 4], 5], [[3, 4], 6]] `````` Reply PUP280 Programmer named Tim Posts: 10 Threads: 3 Joined: Mar 2020 Reputation: 0 May-05-2020, 03:47 PM Thank you perfringo :) Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Creating list of lists from generator object t4keheart 1 123 Nov-13-2020, 04:59 AM Last Post: perfringo Count number of occurrences of list items in list of tuples t4keheart 1 108 Nov-03-2020, 05:37 AM Last Post: deanhystad How do i make a new lists out of an list ozezn1 1 159 Oct-28-2020, 10:19 PM Last Post: Gribouillis concatenating 2 items at a time in a python list K11 3 161 Oct-21-2020, 09:34 AM Last Post: buran Save list with nested list into CSV SpongeB0B 1 222 Oct-12-2020, 07:26 AM Last Post: bowlofred List index out of range error while accessing 2 lists in python K11 2 256 Sep-29-2020, 05:24 AM Last Post: K11 Appending to list of list in For loop nico_mnbl 2 196 Sep-25-2020, 04:09 PM Last Post: nico_mnbl Comparing items from 2 lists of dictionaries illwill 7 242 Sep-14-2020, 10:46 PM Last Post: bowlofred How to make a list of values from a dictionary list? faryad13 2 241 Sep-03-2020, 03:45 PM Last Post: faryad13 Select the other of 2 items in a list Clunk_Head 7 449 Sep-01-2020, 05:27 PM Last Post: Clunk_Head

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020