Python - convert string to list

0 votes

i have a string like this :

states = "Alaska Alabama Arkansas American Samoa Arizona California Colorado"

and I want to split it into a list like this

states = {Alaska, Alabama, Arkansas, American, Samoa, ....}

Jul 25, 2018 in Python by Neha
• 6,180 points
38 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

states.split() will return

['Alaska', 'Alabama', 'Arkansas', 'American', 'Samoa', 'Arizona', 'California', 'Colorado']

If you need one random from them, then you have to use the random module:

import random states = "... ..."

random_state = random.choice(states.split())

answered Jul 25, 2018 by Frankie
• 9,710 points

Related Questions In Python

0 votes
1 answer

Python - convert string to list

try states.split() this would help. READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 56,140 points
45 views
+1 vote
5 answers

convert string to int python

Using list comprehensions: t2 = [map(int, list(l)) for ...READ MORE

answered Oct 18, 2018 in Python by donald
48 views
+1 vote
2 answers

How to convert list to string

mylist = [1, 2, 3] ‘’.join(map(str, mylist)) ==> ...READ MORE

answered Aug 21, 2018 in Python by Omkar
• 66,880 points
30 views
0 votes
1 answer

Convert string list of dict from csv into JSON object in python

You can use the ast module Ex: import ast s = """[{'10': ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 56,140 points
292 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
54 views
+4 votes
6 answers
0 votes
1 answer

How to add to the python path in Windows?

You know what has worked for me ...READ MORE

answered Jul 25, 2018 in Python by Frankie
• 9,710 points
50 views
0 votes
1 answer

How to get the current time in Python

>>> import datetime >>> datetime.datetime.now() datetime(2018, 25, ...READ MORE

answered Jul 25, 2018 in Python by Frankie
• 9,710 points
22 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.