Python string to two dimensionals list [duplicate] Python string to two dimensionals list [duplicate] flask flask

Python string to two dimensionals list [duplicate]


Your expected output is a list of tuples which you can get with zip, str.split and using iter which is more efficient than slicing.

FuntionStr='AccName,S,Balance,N,AccrInterest,N'it = iter(FuntionStr.split(","))print(list(zip(it,it)))[('AccName', 'S'), ('Balance', 'N'), ('AccrInterest', 'N')]

If you have an uneven length list after splitting and you don't want to lose any data you can use zip_longest:

FuntionStr='AccName,S,Balance,N,AccrInterest,N,foo'it = iter(FuntionStr.split(","))from itertools import zip_longestprint(list(zip_longest(it, it,fillvalue="")))]('AccName', 'S'), ('Balance', 'N'), ('AccrInterest', 'N'), ('foo', '')]


To get this:

FuntionStr='AccName,S,Balance,N,AccrInterest,N'my_list = FuntionStr.split(',')print my_listprint zip(my_list[::2], my_list[1::2])

OUTPUT:

[('AccName','S'),('Balance','N'),('AccrInterest','N')]


Try this

temp = FuntionStr.split(',')>>>zip(temp[::2], temp[1::2])[('AccName', 'S'), ('Balance', 'N'), ('AccrInterest', 'N')]