I have a df with some variables as a str like these:
a = '[],[79, 82],[82],[]'
b = '[],[71, 85],[44],[], [], [], [78,99,120,33],[]'
how can i get to:
a_list = [79, 82, 82]
b_list = [71, 85, 44, 78, 99, 120, 33]
I have a df with some variables as a str like these:
a = '[],[79, 82],[82],[]'
b = '[],[71, 85],[44],[], [], [], [78,99,120,33],[]'
how can i get to:
a_list = [79, 82, 82]
b_list = [71, 85, 44, 78, 99, 120, 33]
Start with converting variable to list
import json
correct_json = f"[{a}]" #correct_json is list of lists [[],[79, 82],[82],[]]
l = json.loads(correct_json)
Then flatten the list
a_list = [item for sublist in l for item in sublist]
print(a_list) # [79, 82, 82]
Quick & Dirty Solution:
def collect_list_from_lists_tuple(t: tuple[list]) -> list:
_collection = []
for _sub_collection in t:
for elem in _sub_collection:
_collection.append(elem)
return _collection
I would rephrased it into list comprehensions later.