PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] How to append i'th element of a list to first entry in i'th list in list of lists?

 May 13, 2022     append, list, python     No comments   

Issue

That is: each element in a list ends up as the first element in the corresponding list in a list of lists.

Like the following:

List_of_Lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = [1,2,3]

Resulting:

New_List_of_List = [[1,1,2,3][2,2,3,4],[3,4,4,4]]

I have tried various append and insert methods, but the main problem is that I'm not sure how to mash up individual lists in List_of_Lists and elements in List.


Solution

You can insert the elements at the 0 position

lists = [[1,2,3],[2,3,4],[4,4,4]]
List1 = [1,2,3]
for i in range(len(lists)):
    
     lists[i].insert(0,List1[i])
print(lists)

Output:

[[1, 1, 2, 3], [2, 2, 3, 4], [3, 4, 4, 4]]


Answered By - user15801675
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing