Warning: sizeof(): Parameter must be an array or an object that implements Countable in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php on line 245

Warning: sizeof(): Parameter must be an array or an object that implements Countable in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php on line 245

Warning: Cannot modify header information - headers already sent by (output started at /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php:245) in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 62
Entering a list as an user-input in Python – Coal Geology and Mining: Consulting Services

Entering a list as an user-input in Python

You probably know and use the input() command in Python for interactive data entry. Sometimes, you wish to enter a list. If you use the input() command, it will store the list as a string. Below i will show you how to convert that string to a list using json.

>>> ls=input(‘enter a list’)
enter a list [1,2,3]
>>> ls
‘ [1,2,3]’
>>> type(ls)
<class ‘str’>
>>> import json
>>> str_list=input(‘enter a list’)
enter a list[1,2,3]
>>> str_list
‘[1,2,3]’
>>> my_list=json.loads(str_list)
>>> my_list
[1, 2, 3]
>>> type(my_list)
<class ‘list’>

Leave a Comment

Your email address will not be published. Required fields are marked *