"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Convert Comma-Delimited Strings to Python Lists?

How to Convert Comma-Delimited Strings to Python Lists?

Published on 2024-11-09
Browse:338

How to Convert Comma-Delimited Strings to Python Lists?

Transforming Comma-Delimited Strings to Python Lists

Given a string containing a series of comma-separated values, it is desirable to convert it into a Python list. This transformation aids in further data analysis and manipulation.

To achieve this, leverage the str.split method:

my_string = 'A,B,C,D,E'
my_list = my_string.split(",")
print(my_list)

This yields a list of strings: ['A', 'B', 'C', 'D', 'E']. If a tuple is preferred, simply enclose the list in parentheses:

print(tuple(my_list))

For appending to an existing list, utilize the .append method:

my_list.append('F')
print(my_list)

This appends 'F' to the list, resulting in ['A', 'B', 'C', 'D', 'E', 'F'].

Release Statement This article is reprinted at: 1729402937 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3