Skip to content
Advertisement

How to create and update the same list from another list

i have two lists:

 a= ['a','b','c','d','e','f','4','to','the','words']
 b = [[1,2,3],[1,2,3,4,5],[1,2]]

Now i need another list that updates value like:

 c = ['a','b','c'] w.r.t list of list in b
 c = ['d','e','f','4','to'] #update new list by removing previous one
 c = ['the','words']
 and so on......

How do i update list as mentioned above?

Advertisement

Answer

Your question is not extremely clear but it seems you’re looking for something like this:

You can keep an offset of the index to add elements to new lists based on the indices provided.

a = ['a','b','c','d','e','f','4','to','the','words']
b = [[1,2,3],[1,2,3,4,5],[1,2]]

last_offset = 0
cs = []
for bi in b:
  cs.append([a[bii + last_offset - 1] for bii in bi])
  last_offset += max(bi)
print(cs)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement