-1

I have a txt file that has lists inside.

['livhu','clean','today']
['livhu','cook','tomorrow']

When I use this code attached.

My output is:

['livhu','clean','today']
['livhu','clean','today', 'livhu','cook','tomorrow']

The code Im using

I expected the output to be:

['livhu','clean','today', 'livhu','cook','tomorrow']
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Vha venda
  • 17
  • 1
  • Please include the actual code in your question instead of a picture of it. The exact actual content of the file would be useful too, if what you posted isn't it -- since you didn't post the actual code I can't run it quickly to make sure, but I'd expect the output to be a little bit "rougher" if you put pseudo-JSON into the file and tried to parse it with `str.split()`. – Samwise Mar 17 '23 at 18:30
  • Note that the ***string*** `"['livhu','clean','today']"` is different from the ***list*** `['livhu','clean','today']`. Do you have control over the process that is writing your `.txt` file? If so, I recommend writing the values in a format that is easier to parse, like CSV or JSON. – 0x5453 Mar 17 '23 at 18:32
  • Does this answer your question? [How do I merge multiple lists into one list?](https://stackoverflow.com/questions/11574195/how-do-i-merge-multiple-lists-into-one-list) – Itération 122442 Mar 17 '23 at 18:33

4 Answers4

0

Try to change append to extend

user_lists.extend(user_list)

If that does not help, try to also comment (#) the string above (user_list = " ".join...)

iglebov
  • 25
  • 6
0

You could use:

l = [ eval(item) for item in s.split(' ') ]
Julio S.
  • 944
  • 1
  • 12
  • 26
0

if you are reading a text like

txt = "['livhu','clean','today'] ['livhu','cook','tomorrow']"

you can parse it into two lists using this code

txt1, txt2 = txt.split()

The output will be...

print(txt1)
>> "['livhu','clean','today']"

print(txt2)
>> "['livhu','cook','tomorrow']"

note that the output still a string to convert it into list of strings you can use function like this

def convert_to_list(string:str) -> list:
    string = string.replace("[", "").replace("]", "") # to replace square brackets "[" and "]"
    string = string.replace("'", "") # to replace the single quotes '
    return [i for i in string.split(",")]

you can use this function as follow..

li1 = convert_to_list(txt1)
li2 = convert_to_list(txt2)

the output will be..

print(li1)
>> ['livhu','clean','today']

print(li2)
>> ['livhu','cook','tomorrow']

finally you can combine them into one list using this code

li = list1 + list2

The output will be

print(li)
>> ['livhu','clean','today','livhu','cook','tomorrow']

but note that there is assumption here as we assume that the text will contain one space, if there is more spaces you will need more variables to unpack the text.

  • converion only work of list of string with no string having `[` or `]` character except from first and last . just do exec(stirng) that's better, save that in object – sahasrara62 Mar 17 '23 at 20:36
0

I think you maybe want a unique output as a result. You can try a merging the list then cast it to a set then cast the results back again to a list. Try this:

list1 = ['livhu','clean','today']
list2 = ['livhu','cook','tomorrow']
combined = list(set(list1 + list2))
print(combined)
>>> ['today', 'clean', 'livhu', 'tomorrow', 'cook']

From your code, you can cast user_list to a set to get unique values, then cast it back again to a list:

user_list = list(set(user_list))
Daniel Afriyie
  • 217
  • 4
  • 12