-1

I have a list as follows:

listMain = [ [ (0, str) ], [ (0, str), (1, str) ], [ (1, str), (1, str), (2, str) ], 
           ... ]

Next I have a integer value, lets call it value=4

Now I want to build a new list that looks as follows:

listMain_New = [ [ (4, str) ], [ (4, str), (5, str) ], [ (5, str), (5, str), (6, str) ], ... ]

Basically, I want the numbers 0,1,2,.. in listMain replaced with value, value+1, value+2 in listMain_New.

Can someone help me how to do this in python ?

user91
  • 11
  • 2
  • It's not clear whether you want to *filter out* the tuples which contain a number less than `value` or if you want to *add* `value` to all the tuples. – ddejohn Feb 14 '21 at 19:50
  • It seems like a first one. Example is slightly missleading. – Robert Axe Feb 14 '21 at 19:52
  • 2
    @Epsi95 that doesn't work for OP's code. The structure of their data is `List[List[Tuple[int, str]]]`. Some inner lists contain more than one tuple. You should test your code before commenting, as yours raises a "not supported" error. – ddejohn Feb 14 '21 at 19:53
  • What have you already tried, and where are you stuck? This is pretty basic stuff. Do you know how to do a [nested list comprehension](https://stackoverflow.com/q/18072759/4518341)? – wjandrea Feb 14 '21 at 21:51

1 Answers1

2

Just write a loop in loop

value = 4
listMain_New = [[(i, t) for i, t in inner if i >= value] for inner in outer]
Robert Axe
  • 396
  • 2
  • 11
  • Thank you. But it is not clear to me. what I want is, the numbers 0,1,2,.. in listMain replaced with value, value+1, value+2 in listMain_New – user91 Feb 14 '21 at 20:32