How to convert a string of strings into a list?

Related searches

I have the string

"Abarth", "AC", "Aixam", "Ak", "Alfa%20Romeo", "Alpine", "Ariel", "Aston%20Martin", "Audi", "Austin", "Bac", "Beauford", "Bentley", "BMW", "Bristol", "Bugatti", "Buick", "Cadillac", "Caterham", "Chesil", "Chevrolet", "Chrysler", "Citroen", "Corvette", "Cupra", "Custom%20Vehicle", "Dacia", "Daewoo", "Daihatsu", "Daimler", "Datsun", "DAX", "Dodge", "DS%20AUTOMOBILES", "Ferrari", "Fiat", "Ford", "GMC", "Great%20Wall", "Holden", "Honda", "Humber", "Hummer", "Hyundai", "Infiniti", "Isuzu", "Iveco", "Jaguar", "Jeep", "Jensen", "Kia", "Koenigsegg", "KTM", "Lamborghini", "Lancia", "Land%20Rover", "Levc", "Lexus", "Leyland", "Lincoln", "Lotus", "Maserati", "Maybach", "Mazda", "McLaren", "Mercedes-Benz", "MG", "MINI", "Mitsubishi", "Morgan", "Morris", "Nissan", "Noble", "Opel", "Perodua", "Peugeot", "Pilgrim", "Plymouth", "Pontiac", "Porsche", "Proton", "Radical", "Raw", "Reliant", "Renault", "Replica", "Riley", "Robin%20Hood", "Rolls-Royce", "Rover", "Saab", "SEAT", "SKODA", "Smart", "Ssangyong", "Standard", "Subaru", "Sunbeam", "Suzuki", "Tesla", "Tiger", "Toyota", "Triumph", "TVR", "Vauxhall", "Volkswagen", "Volvo", "Westfield", "Yamaha", "Zenos"

which is stored by the variable "list_of_car_makes" in my python project

but I want to convert the string into a list, how would I do that? when I use print(list_of_car_makes[0]) it prints the first character in the string, the ", what i'd like to be able to print is Abarth when using that print function.

Okay you have other weird characters in there what I'm going to ignore (%20 is a space, but you can work out how to fix that)

Your issue to the letter would be solved by:

your_string_here.replace('"', '').split(',')

Python, Python Convert String to List, How to convert string to list of characters in If you are not familiar with f-prefixed string formatting, please read f-strings in Python. ini_list = " [1, 2, 3, 4, 5]" # printing initialized string of list and its type. print ("initial string", ini_list) print (type(ini_list)) # Converting string to list. res = ast.literal_eval (ini_list) # printing final result and its type.

Try this:

makes = []
word = ""
for c in list_of_car_makes:
    if c in " ,":

    if c == '"':
        if word:
            word = ""

    word += c



['Abarth', 'AC', 'Aixam', 'Ak', 'Alfa%20Romeo', 'Alpine', 'Ariel', 'Aston%20Martin', 'Audi', 'Austin', 'Bac', 'Beauford', 'Bentley', 'BMW', 'Bristol', 'Bugatti', 'Buick', 'Cadillac', 'Caterham', 'Chesil', 'Chevrolet', 'Chrysler', 'Citroen', 'Corvette', 'Cupra', 'Custom%20Vehicle', 'Dacia', 'Daewoo', 'Daihatsu', 'Daimler', 'Datsun', 'DAX', 'Dodge', 'DS%20AUTOMOBILES', 'Ferrari', 'Fiat', 'Ford', 'GMC', 'Great%20Wall', 'Holden', 'Honda', 'Humber', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Iveco', 'Jaguar', 'Jeep', 'Jensen', 'Kia', 'Koenigsegg', 'KTM', 'Lamborghini', 'Lancia', 'Land%20Rover', 'Levc', 'Lexus', 'Leyland', 'Lincoln', 'Lotus', 'Maserati', 'Maybach', 'Mazda', 'McLaren', 'Mercedes-Benz', 'MG', 'MINI', 'Mitsubishi', 'Morgan', 'Morris', 'Nissan', 'Noble', 'Opel', 'Perodua', 'Peugeot', 'Pilgrim', 'Plymouth', 'Pontiac', 'Porsche', 'Proton', 'Radical', 'Raw', 'Reliant', 'Renault', 'Replica', 'Riley', 'Robin%20Hood', 'Rolls-Royce', 'Rover', 'Saab', 'SEAT', 'SKODA', 'Smart', 'Ssangyong', 'Standard', 'Subaru', 'Sunbeam', 'Suzuki', 'Tesla', 'Tiger', 'Toyota', 'Triumph', 'TVR', 'Vauxhall', 'Volkswagen', 'Volvo', 'Westfield', 'Yamaha', 'Zenos']

To remove the %20 (and other codes, add this bit of code):

for j in range(len(makes)):
    while "%" in makes[j]:
        i = makes[j].find("%")
        s = int(makes[j][i+1:i+3], base=16)
        m = list(makes[j])
        del m[i:i+3]
        m.insert(i, chr(s))
        makes[j] = ''.join(m)

Output with this code:

['Abarth', 'AC', 'Aixam', 'Ak', 'Alfa Romeo', 'Alpine', 'Ariel', 'Aston Martin', 'Audi', 'Austin', 'Bac', 'Beauford', 'Bentley', 'BMW', 'Bristol', 'Bugatti', 'Buick', 'Cadillac', 'Caterham', 'Chesil', 'Chevrolet', 'Chrysler', 'Citroen', 'Corvette', 'Cupra', 'Custom Vehicle', 'Dacia', 'Daewoo', 'Daihatsu', 'Daimler', 'Datsun', 'DAX', 'Dodge', 'DS AUTOMOBILES', 'Ferrari', 'Fiat', 'Ford', 'GMC', 'Great Wall', 'Holden', 'Honda', 'Humber', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Iveco', 'Jaguar', 'Jeep', 'Jensen', 'Kia', 'Koenigsegg', 'KTM', 'Lamborghini', 'Lancia', 'Land Rover', 'Levc', 'Lexus', 'Leyland', 'Lincoln', 'Lotus', 'Maserati', 'Maybach', 'Mazda', 'McLaren', 'Mercedes-Benz', 'MG', 'MINI', 'Mitsubishi', 'Morgan', 'Morris', 'Nissan', 'Noble', 'Opel', 'Perodua', 'Peugeot', 'Pilgrim', 'Plymouth', 'Pontiac', 'Porsche', 'Proton', 'Radical', 'Raw', 'Reliant', 'Renault', 'Replica', 'Riley', 'Robin Hood', 'Rolls-Royce', 'Rover', 'Saab', 'SEAT', 'SKODA', 'Smart', 'Ssangyong', 'Standard', 'Subaru', 'Sunbeam', 'Suzuki', 'Tesla', 'Tiger', 'Toyota', 'Triumph', 'TVR', 'Vauxhall', 'Volkswagen', 'Volvo', 'Westfield', 'Yamaha', 'Zenos']

Python Convert String to List, The following Python code will turn your string into a list of strings: import ast teststr = "['aaa','bbb','ccc']" testarray = ast.literal_eval(teststr). If we want to convert string to list operation, then we need to use the Python string split() method. Python split() method is used to split the strings and store them in the list . The split() method returns a list of the words in the string, using the “delimiter” as the delimiter string.

I would suggest splitting by your delimiter then using str.split as you can provide it with characters to trim from the ends of a string.

[s.strip('"') for s in your_string.split(', ')]

You can even replace %20 with a space this way.

[s.strip('"').replace('%20', ' ') for s in your_string.split(', ')]

Try it here!

How to convert a string with comma-delimited items to a list in Python?, This isn't as good as the ast solution, for example it doesn't correctly handle escaped quotes in strings. But it's simple, doesn't involve a dangerous eval, and� How to create a tuple from a string and a list of strings in Python? How to sort a list of strings in Python? How to remove empty strings from a list of strings in Python? How to remove an empty string from a list of empty strings in C#? C# Program to search for a string in an array of strings; How convert an array of Strings in a single String

How to convert string representation of list to a list?, Strings in Python are iterable. That means you can use them as a list and get one character at a time. For example: for character in my_string� Use the map function (in Python 2.x):. results = map(int, results) In Python 3, you will need to convert the result from map to a list:. results = list(map(int, results))

In Python, how do I convert a string to a list in which each character , Method #1 : Naive Method This is most generic method that strikes any programmer while performing this kind of operation. Just looping over whole list and convert each of the string of list to int by type casting.

Given a list, write a Python program to convert the given list to string. There are various situation we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.

  • How are you creating this string in the first place?
  • This is definetly an XY problem. From where are you getting this string? There must be a way to avoid that massive blunder
  • Also consider if there are double quotes within the strings. You lose them this way.
  • You legend, that works perfect! Thank you very much. As an fyi, I need the %20 in there and had to do some .replace trickery to get them there in the first place.
  • Glad to have helped :)