Python Forum

Full Version: removing quotes from a list and keep type list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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]
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]
When dealing with IP addresses it’s might be good idea to use built-in ipaddress module.