Quick links
Latest articles
Internship
Members
New...
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

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]





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



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 (2015-2018), Some rights reserved.