Python Forum
Add items from one list to list of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add items from one list to list of lists
#1
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


Messages In This Thread
Add items from one list to list of lists - by PUP280 - Mar-24-2020, 10:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 179 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,156 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,321 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 871 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,064 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,058 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,611 Oct-01-2022, 07:15 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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