onlyforbopi
12/12/2016 - 8:02 AM

## ListConversions.py

ListConversions.py

``````a = [1,2,3,4,5,6,7,8,9]

def unflatten_list(lista, grouping):
'''
Function : unflatten_list
Description : Converts a list into a list of lists
Inputs : List (Simple)
grouping (How many values grouped together)
Output : List of Lists (Based on grouping)
Usage(print): print (unflatten_list(a,3))
Usage(Assign): b = unflatten_list(a,3))
'''
i=0
new_list=[]
while i<len(lista):
new_list.append(lista[i:i+grouping])
i+=grouping
return new_list

print (unflatten_list(a, 2))``````
``````

def tuple2list(tuple):
'''
Function : tuple2list
Description : Converts a tuple into a list
Inputs : Tuple
Output : List
Usage(print): print (tuple2list(tuple))
Usage(Assign): b = tuple2list(tuple))
'''
outlist = []
for j in tuple:
outlist.append(j)
return outlist

``````
``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

def flatten_list(lists):
'''
Function : flatten_list
Description : Converts a list of lists into a list
Inputs : List of Lists
Output : List
Usage(print): print (flatten_list(Lists))
Usage(Assign): b = flatten_list(Lists))
'''
results = []
for numbers in lists:
for j in numbers:
results.append(j)
return results

print flatten(n)``````
``````def list2tuple(lista):
'''
Function : list2tuple
Description : Converts a list into a tuple
Inputs : List
Output : Tuple
Usage(print): print (list2tuple(list))
Usage(Assign): b = list2tuple(list))
'''
outtup = ()
for j in lista:
outtup = outtup + (j,)

return outtup

a = [1,2,3,4,5]

print (list2tuple(a))``````