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
#5
(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]]
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Messages In This Thread
RE: Add items from one list to list of lists - by pyzyx3qwerty - May-02-2020, 01:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting a list of lists by an element leapcfm 3 168 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  Why changing data in a copied list changes the original list? plumberpy 3 331 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  behavior list of lists roym 5 351 Jul-04-2021, 04:43 PM
Last Post: roym
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 893 Jun-10-2021, 08:35 PM
Last Post: perfringo
  Feed List items with Integer euras 9 836 May-19-2021, 07:45 PM
Last Post: snippsat
  List of lists - merge sublists with common elements medatib531 1 757 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 590 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Parse String between 2 Delimiters and add as single list items lastyle 5 753 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Order a list with successive permutations based on another list yvrob 3 733 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 866 Mar-10-2021, 09:58 AM
Last Post: quest_

Forum Jump:

User Panel Messages

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