1

Hello guys I am having some issue trying to split my list into multiple element, my problem is the following: I have a list like that:

list1 = ['"22.23.24.25"']

(basically this list should represent an IP address but never mind) the problem is that I want to delete the quotation mark currently into my list, ideally my list would like:

list1 = ['22.23.24.25']

I really don't know how to do it since I am a beginner in python so it would be great if anyone can help me.

jgritty
  • 11,660
  • 3
  • 38
  • 60
maxpython
  • 73
  • 1
  • 6

5 Answers5

4

Simple solution is use the str.replace method twice, to ensure that all types of the quotations are removed.

list1 = ['"22.23.24.25"', "'11.22.33.44'"]

list1 = [x.replace('"', '').replace("'", '') for x in list1]

print(list1)
['22.23.24.25', '11.22.33.44']
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
2

Replacing in all elements in a list:

a=[i.replace("\"","") for i in list1]

result:

['22.23.24.25']

this iterates over all elements in list1, performs an action on each and puts them into new list

Adam Sałata
  • 154
  • 2
  • 13
2

Just for the collection:

list1 = ['"22.23.24.25"', "'11.22.33.44'", "11.22.33.44"]

new_list = "\n".join(list1).replace("\"","").replace("'","").split("\n")

print(new_list) # ['22.23.24.25', '11.22.33.44', '11.22.33.44']
  1. Join the array in one long string.
  2. Remove all quotes from the string.
  3. Split the sting back into an array.

This is a quite common technique. And probably it works faster than a list comprehension.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
1
list1 = ['"22.23.24.25"']
for i in range(len(list1)):
    x=list1[i].replace("\"","")
Eric Hogue
  • 11
  • 1
1
l1=['"10.40.30.40.50"']
for x in l1:
    s=str(l1[0])
    for n in s:
        value=s[1:len(s)-1]
print(value)

Just a another method.

Brown Bear
  • 19,655
  • 10
  • 58
  • 76