0

I was trying to solve this problem:

Write a program that takes two lists and returns a list that contains all the elements of the first list minus all the common elements between the two lists.

The coding part is very simple. Here it is:

list1=input()
list2=input()

for i in list1:
  if i in list2:
    list1.remove(i)
  else:
    pass

print(list1)

The problem that I face here is that list1 and list2 are strings. Take list1=‘[1,2,3,4]’.

I need to convert list1 to [1,2,3,4].

I tried split() and join() methods as suggested in How to convert list to string but I failed.

How do I convert '[1,2,3,4]' to [1,2,3,4]?

Aaryan Dewan
  • 204
  • 3
  • 11
  • Probably acceptable for this use case: http://pythoncentral.io/cutting-and-slicing-strings-in-python/ – fdsa Jul 17 '17 at 01:43
  • 1
    there is an answer for this [here](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python). please search your problem before asking a new question. – sam-pyt Jul 17 '17 at 01:50
  • @9.0 I am so sorry. I did google search the problem but I could not find any solutions. My bad – Aaryan Dewan Jul 17 '17 at 02:10

3 Answers3

3

Eval the string using the ast module, which is safe:

import ast
ast.literal_eval('[1, 2, 3, 4]')
=> [1, 2, 3, 4]
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • What do you mean by ‘safe’? Can’t I use json.loads() which is surprisingly quicker than ast.literal_eval()? – Aaryan Dewan Jul 17 '17 at 02:00
  • 1
    @AaryanDewan safe as opposed to using `eval()`, which would be another (but unsafe) option - `eval()` doesn't validate its input (which could contain malicious code) whereas `literal_eval()` does. See this [post](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval) for details. – Óscar López Jul 17 '17 at 02:03
2

You have two options, you can load it as json:

import json
json.loads('[1,2,3,4]')
# [1, 2, 3, 4]

Or you can evaluate the string with ast.literal_eval

from ast import literal_eval    
literal_eval('[1,2,3,4]')
# [1, 2, 3, 4]
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Thanks for the answer! Is there any benefit for using one of them over the other? – Aaryan Dewan Jul 17 '17 at 01:47
  • 1
    `json.loads()` is surprisingly quicker than `ast.literal_eval()` (Py3.6). – AChampion Jul 17 '17 at 01:52
  • 2
    They are slightly different. Except for the performance side @AChampion just noted. The json.loads version is more restrict about the quotes, for instance you can not have single quote around a value because json doesn't accept single quote as valid quoting characters. So ast.literal_eval might be a little more general here. – Psidom Jul 17 '17 at 01:54
-3

i would convert them right on input in this case you dont even need to use commas

list1=list(input())
list2=list(input())
oToMaTiX
  • 119
  • 7
  • That would cause a bug. I have tried that – Aaryan Dewan Jul 17 '17 at 01:45
  • 1
    This will not work, `input()` returns a string the list would contain all the characters in the string, not just the values, e.g. `["'", "[", "1", ",", ...]` not what is being looked for. – AChampion Jul 17 '17 at 01:47
  • what kind? i just tried ['1', '2', '3', '4'] this was return in inpout 1234 – oToMaTiX Jul 17 '17 at 01:47
  • @oToMaTiX You are probably using Python 2, where `input` passes the input string to `eval` first (making the call to `list` unnecessary). Python 3's `input` is the equivalent of `raw_input`. – chepner Jul 17 '17 at 02:30