Home » Python programs

Python | Input comma separated elements, convert into list and print



Here, we are going to learn how to convert comma separated elements into list using Python? Here, we will input comma separate elements and convert into a list of integers.
Submitted by IncludeHelp, on August 08, 2018

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:

# input comma separated elements as string 
str = str (raw_input ("Enter comma separated integers: "))
print "Input string: ", str

# conver to the list
list = str.split (",")
print "list: ", list

# convert each element as integers
li = []
for i in list:
	li.append(int(i))

# print list as integers
print "list (li) : ", li

Output

    Enter comma separated integers: 10,20,30,40,50
    Input string:  10,20,30,40,50
    list:  ['10', '20', '30', '40', '50']
    list (li) :  [10, 20, 30, 40, 50]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Quick links
Latest articles, Internship, Members
New...
Coding problems, Algorithms, Discrete Mathematics, Big data
Languages
C, C++, C++ STL, Java, Data Structure, C#.Net, Android, Kotlin, SQL
Web
PHP, Python, JavaScript, CSS, Ajax, Node.js, Web prog.
Programs
C, C++, DS, Java, C#, Python
Aptitude
C, C++, Java, DBMS
Interview
C, Embedded C, Java, SEO, HR
CS Subjects
CS Basics O.S. Networks DBMS Embedded Systems Cloud Computing Machine learning CS Organizations Linux DOS
More...
Articles, Puzzles, News/Updates


Recommended posts
C Tips & Tricks, C++ Tips & Tricks
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distros of 2018
C programming optimization techniques
Differences b/w C & Embedded C?
Embedded C Interview Q. & A.
C programming tips for Embedded Development
Basic rules of writing a C program
Important points (rules) to remember while writing C/C++ program
Top 5 Websites for solving programming challenges
Read more...


Others...
Computer G.K. (MCQ)
Most viewed pages...
Categories...



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.