0

In the func function, I performed some processing on the oldlist[] and then saved the result in a buffer[] list. Now I want to save this buffer[]list to be the first one in a list of tuples. After that, a new values in buffer[] must be saved as a second list in tuples and so on. The output something like this

#list of tuple:
Ltuple=[(1,2,3),(66,53,6),(3,1,5,8,3)]

I do not know how to use list of tuple any advice please?

def func(oldlist):
    buffer = []
    buffer.append(oldlist[0])
    # some processing on the items of oldlist[]  
        buffer.append(oldlist[index])
    print(buffer)
    # Now here I need to append the buffer[] in list of tuple to be the
    #  first list in tuple and in next time the new list insert to be 
    #  the second list in list of tuple
    buffer.clear()


oldlist=[]
#Some code to deal with oldlist[] and in each time send some items
# from oldlist[] to func() 

func(oldlist)
oldlist.clear()
lena
  • 730
  • 2
  • 11
  • 23
  • In fact, I needed to deal with this thing, but I do not have any idea how to append the lists in list of tuple – lena Jan 24 '21 at 20:39
  • I update my question with more information, I hope you understand what I want – lena Jan 24 '21 at 21:27
  • I don't, sorry. Are you trying to [rotate the list](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python)? Please show actual/expected output and a [mcve] along with a complete specification for the behavior you need. Thanks. – ggorlen Jan 24 '21 at 21:28
  • Simply, the buffer[] list in each time it will contain a group of numbers (a small list). I need to insert the list in the list of tuple then clear buffer[] to content new items. Next the new content of buffer[] append in tuple to be something like this Ltuple=[(1,2,3),(66,53,6),(3,1,5,8,3)] – lena Jan 24 '21 at 21:39
  • Why not show both lists verbatim before and after the function? – ggorlen Jan 24 '21 at 21:47
  • Because they are the same lists, my problem is not dealing with lists, but the problem in inserting the list inside tuple! – lena Jan 24 '21 at 21:52
  • Huh. OK. Well, both lists are cleared in your example, so yeah, I really don't see what operation is supposed to happen. Whatever the structure is, if you can't show it before and after whatever the operation is, I'm at a loss, so I'll step aside and let someone else help you. Good luck. – ggorlen Jan 24 '21 at 21:54

1 Answers1

0

Can you pass Ltuple to the function? If not it has to be made global or you need to return the buffer tuple I have modified the function below to take the list of tuples also.

def func(oldlist, list_of_tuples):
    buffer = []
    buffer.append(oldlist[0])
    # some processing on the items of oldlist[]  
        buffer.append(oldlist[index])
    print(buffer)
    # Now here I need to append the buffer[] in list of tuple to be the
    #  first list in tuple and in next time the new list insert to be 
    #  the second list in list of tuple
    list_of_tuples.append(tuple(buffer))
    buffer.clear()

oldlist=[]
#Some code to deal with oldlist[] and in each time send some items
# from oldlist[] to func()

list_of_tuples = []
func(oldlist, list_of_tuples)
oldlist.clear()
lllrnr101
  • 2,288
  • 2
  • 4
  • 15
  • Thank you so much, I have other question please, how can get the first list in this list of tuples and the second one and so on, first list, not item in list – lena Jan 25 '21 at 09:32
  • Use append. It can be used to append entire list not just individual items. – lllrnr101 Jan 25 '21 at 09:49
  • ```>>> list_example = []``` ```>>> list_example.append((1,2,3,4,))``` ```>>> list_example.append((5,6,7,8))``` ```>>> list_example.append((10,12))``` ```>>> list_example``` ```[(1, 2, 3, 4), (5, 6, 7, 8), (10, 12)]``` ```>>> ``` – lllrnr101 Jan 25 '21 at 09:50
  • No no, something like array for example when typing list_of_tuples[i], it should return the first list (1,2,3), how can do that ? – lena Jan 25 '21 at 10:33
  • The way you wrote. list_of_tuples[i] will return the ith tuple in the list. – lllrnr101 Jan 25 '21 at 10:35
  • yes, but this return all values in tuples, I need to get firat list in tuple and next time second list, and so on :) – lena Jan 25 '21 at 10:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227786/discussion-between-lllrnr101-and-oleva). – lllrnr101 Jan 25 '21 at 10:51