Python Forum
removing quotes from a list and keep type list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: removing quotes from a list and keep type list (/thread-20277.html)



removing quotes from a list and keep type list - evilcode1 - Aug-03-2019

hello all ..
im trying to use sys.argv take and ip like : 192.168.1.99 and convert it to [192.168.1.99] ... but it add '
import sys

start = sys.argv[1]
start = start.replace(".", ",")
start = [start]
print(start)
Output:
['85,8,8,8']
i need to remove ' from it and print it like : [85,8,8,8] but i need to keep the type (list) not str
how i can do that


RE: removing quotes from a list and keep type list - SheeppOSU - Aug-03-2019

You still have the type list, but the value inside is string. So you want to change the value inside to multiple int numbers from my interpretation. Here's how that can be done
List = ['85, 8, 8, 8']
output = []

for number in List[0].split(','):
    while ' ' in number:
		number = number.replace(' ', '')
    output.append(int(number))
print(output)
Output:
[85, 8, 8, 8]



RE: removing quotes from a list and keep type list - buran - Aug-03-2019

start will be a str, so you can do something like

use map
>>> start = '192.168.1.99'
>>> start = list(map(int, start.split('.'))) # you need to convert the map object to list
>>> start
[192, 168, 1, 99]
or use list comprehension
>>> start = '192.168.1.99'
>>> start = [int(num) for num in start.split('.')]
>>> start
[192, 168, 1, 99]



RE: removing quotes from a list and keep type list - perfringo - Aug-03-2019

When dealing with IP addresses it’s might be good idea to use built-in ipaddress module.