Input comma separated elements (integers), and converts it into list in Python.
Example:
Input:
Enter comma separated integers: 10,20,30,40,50
Output:
list: ['10', '20', '30', '40', '50']
List (after converted each element to int)
list (li) : [10, 20, 30, 40, 50]
Logic:
- Input a comma - separated string using raw_input() method.
- Split the elements delimited by comma (,) and assign it to the list, to split string, use string.split() method.
- The converted list will contains string elements.
- Convert elements to exact integers:
- Traverse each number in the list by using for...in loop.
- Convert number (which is in string format) to the integer by using int() method.
- Print the list.
Program:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer