Samer Haidar
Samer Haidar

Reputation: 1

How to flatten a list of lists formed of strings?

I have a list formed of lists that contains strings. It looks like this:

["['eth', 'ethereum', 'nft', 'nonfungibletoken', 'token', 'crypto', 'digitalassets', 'etf', 'digitalcoin', 'ewallet', 'digitalgold', 'internetmoney', 'defi', 'decentralizedfinance', 'peertopeer', 'digitalcurrency', 'decentralizedmoney']",
 "['nft']",
 "['coinvotecc', 'craftnft']",
 "['coinvotecc', 'craftnft']",
 "['fitoken', 'fnk', 'nft', 'bsc', 'bnb']",
 "['nft', 'nft', 'nftarts', 'nftart', 'token', 'opeansea', 'nftcollecting', 'nftbuy', 'nfthorse', 'nftlion', 'nftold', 'nftlionesstiger', 'lion', 'lions']",
 "['nft']",]

I'm not sure what the '"' is for.

I'm trying to flatten it by transforming it into one big list. I've tried a lot of different ways to do so, but i'm getting errors like: 'Float objects are not iteratable' and 'str have no attribute len()'.

I'm not sure what the error is. Any help would be much appreciated.

Upvotes: 0

Views: 104

Answers (3)

Eden Yosef
Eden Yosef

Reputation: 39

What you have inside the big list is a couple of strings. what you can do is just:

big_list = []
for s in lst:
    big_list += s[0][1:-1].split(', ')

it will take the strings inside the list, and will make each of them in to a list of strings, and then add them append them to a big list, such that all of the individual strings will be inside it.

Upvotes: 0

Trevis
Trevis

Reputation: 568

Your list doesn't contain a list but a single string. So you must first convert it to a list, with eval being the simplest:

the_list = ["['eth', 'ethereum', 'nft', 'nonfungibletoken', 'token', 'crypto', 'digitalassets', 'etf', 'digitalcoin', 'ewallet', 'digitalgold', 'internetmoney', 'defi', 'decentralizedfinance', 'peertopeer', 'digitalcurrency', 'decentralizedmoney']"]

eval(the_list[0])

Will give you

['eth', 'ethereum', 'nft', 'nonfungibletoken', 'token', 'crypto', 'digitalassets', 'etf', 'digitalcoin', 'ewallet', 'digitalgold', 'internetmoney', 'defi', 'decentralizedfinance', 'peertopeer', 'digitalcurrency', 'decentralizedmoney']

Edit: If you have several strings (per your edit), you can do

[eval(item) for item in the_list]

Edit 2: If some elements are not strings (per your subsequent comments), you could use this (and I included a way to flatten):

def eval_or_keep(element):
    return eval(element) if isinstance(element, str) else [element]


list_of_lists = [eval_or_keep(item) for item in the_list]

flattened_list = [item 
                  for sub_list in list_of_lists 
                  for item in sub_list]

Upvotes: 1

chatax
chatax

Reputation: 998

You can use the build in itertools module in Python. Using chain.from_iterable you can flatten all iterables within one iterable.

from itertools import chain 


def eval_value(value):
    # Note: if str_lst is an empty string it will give a SyntaxError: unexpected EOF while parsing
    evaluated = eval(value)
    if not isinstance(evaluated, list):
        return [evaluated]
    else:
        return evaluated

    
eval_strings = [eval_value(str_lst) for str_lst in the_list if isinstance(str_lst, str)]
flat = list(chain.from_iterable(eval_strings))

Upvotes: 0

Related Questions