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'].
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