Python Forum
Write a for loop on list of lists without changing the shape of the main list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write a for loop on list of lists without changing the shape of the main list
#1
So I am new to python and am having difficulties on writing an efficient for loop on a list of lists. I have a list called "mylist" and I need to store the values of the first column of sublists, and for that I have written the following:

newlist = []
for i in range(5):
    for j in range(10):
        d = [x for x in mylist[i][j][:,0]]
        newlist.append(d)
The problem is the the answer I get is a list with "len = 50"; meanwhile I need to keep the shape of the "newlist" similar to "mylist" i.e. I want the final answer to be a list of 5 lists, and then in each list there should be 10 other lists. (Please see the attached files; I need to get something like "wanted_list"). I tried different things like:

newlist = []
for i in range(5):
    for j in range(10):
        d[i] = [x for x in mylist[i][j][:,0]]
        newlist.append(d)
or:

newlist = []
for i in range(5):
    for j in range(10):
        d = [x for x in mylist[i][j][:,0]]
    newlist.append(d)
But obviously non of these work. I was wondering if you could teach me how I can do this!

Attached Files

Thumbnail(s)
           
Reply
#2
When you access mylist[i][j] it returns the single item at that position.
Try something like this (if you really need to loop into every item):

newlist = []
for i in range(len(mylist)):
    d = []
    for j in range(len(mylist[i])):
        d.append(mylist[i][j])
    newlist.append(d)
To get only the first item(column):

newlist = []
for i in range(len(mylist)):
    newlist.append([mylist[i][0]])
Reply
#3
(Jun-18-2018, 10:51 PM)gontajones Wrote: When you access mylist[i][j] it returns the single item at that position.
Try something like this (if you really need to loop into every item):

newlist = []
for i in range(len(mylist)):
    d = []
    for j in range(len(mylist[i])):
        d.append(mylist[i][j])
    newlist.append(d)
To get only the first item(column):

newlist = []
for i in range(len(mylist)):
    newlist.append([mylist[i][0]])

Thanks a bunch I got the idea now! :)
Reply
#4
It's much better to loop over the items in the list than to loop of the indexes of the list:

new_list = []
for sub_list in mylist:
    new_list.append([])
    for item in sub_list:
        new_list[-1].append(item)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output shape problem with np.arange alan6690 5 610 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 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,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,564 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  simplekml change shape&color issac_n 2 2,772 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil

Forum Jump:

User Panel Messages

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