Reputation: 23099
I have the following structure,
lists_ = [a,b,c,d,e,f]
within each list there are variables i'd like to access iterate and pass into a list.
new_lists_ = [a1,b1,c1,d1,e1,f1] # empty individual lists.
so from lists_
i'd like to acess all the variables in a
and pass these into a1
with what I'm hoping is a simple one liner.(however I prefer readability!)
my own attemp :
for list_ in lists_:
for new_list_ in new_lists_:
for l in list_:
new_list.append(l)
^ this adds all the items to all the lists so not what I'm after.
naturally, I could access each list individually and iterate this way, but I feel like this isn't good practice. I'm unsure if the above is good or bad practice so any advice is appreciated.
(also my first non pandas question, so I guess this is a milestone..?!)
Upvotes: 1
Views: 67
Reputation: 19414
I think that what you are looking for is copy.deepcopy
:
from copy import deepcopy
new_lists_ = deepcopy(lists_)
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Or going with your method, maybe you should iterate indices instead:
for i in range(len(lists_)):
for elem in lists_[i]:
new_lists_[i].append(elem)
Or as requested, a one-liner:
new_lists_ = [[elem for elem in lists_[i]] for i in range(len(lists_))]
Note: that in the first method, of the explicit loop, you have to make sure that all sub-lists are initialized or you will get an IndexError
. In the second method of the list-comprehension we simply create a new list.
Upvotes: 2
Reputation: 1
I would utilize the while loop in order to iterate over a list of lists. Here is an example but instead of the print statement you could alter list elements.
a = [1,2,3] #example elements
b = [4,5,6]
c = [7,8,9]
lists_ = [a,b,c]
new_lists_ = []
i = 0
while i < len(lists_):
for x in lists_[i]:
print(x)
i+=1
this will output 1-9 as each element has been looked over.
Upvotes: -1
Reputation: 468
Nested loops are things you generally want to avoid.
If you really wanted to stick with a super-uber pythonic way of doing this with nested loops, this should work:
new_lists_ = []
for list_ in lists_:
new_list_ = []
for l in list_:
#some computation on l, stored in stuff, and appended to new_list_
new_list_.append(stuff)
new_lists_.append(new_list_)
But this is awkward to read and follow when debugging.
One way to help debug would be to use the enumerate(item) method in your python loops.
But an even better way, in my opinion, is to split this processing (and nested looping) off into another method, like so:
def some_function(list_):
#do some stuff here, store in processed_list_
processed_list_ = []
for l in list_:
processed_list_.append(l) #you'll want to modify l, of course
return processed_list_
new_lists_ = []
for list_ in lists_:
new_lists_.append(some_function(list_))
much simpler, I think.
If you're following some special style guide, then ignore this, but I would also recommend rethinking how you're naming variables, specifically with your appending a "_" underscore to every variable name. Following some sort of popular python style guide, like the google one here, is a good idea for two main reasons:
Upvotes: 1