2020年12月19日星期六

How to convert each pair of list into tuple in python

I want to convert list = [1,4,2,3,0] to list_tup = [(1,4),(4,2),(2,3),(3,0)]. You can see my code below but it outputs [(1,4),(2,3)]. I am wondering how to adjust indices in zip.

list=[1,4,2,3,0]  list_tup = tuple(zip(list[0::2], list[1::2]))  


from Recent Questions - Stack Overflow https://stackoverflow.com/questions/65376945/how-to-convert-each-pair-of-list-into-tuple-in-python Aras

没有评论:

发表评论