-1

Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.

values = input("Input some comma separated numbers : ")

list = values.split(",")

tuple = tuple(list)
print('List : ',list)


print('Tuple : ',tuple)

This does work but is there any other easier way?

karthikr
  • 97,368
  • 26
  • 197
  • 188
Ariel Waters
  • 97
  • 1
  • 5
  • 6
    How much easier do you want that to get? It's pretty straight forward. The only comment I'd make is that you shouldn't use `list` and `tuple` in python code as variables because then you can no longer make a list or a tuple with them – sedavidw Nov 12 '15 at 16:13

2 Answers2

0

If you're looking for a more efficient way to do this, check out this question: Most efficient way to split strings in Python

If you're looking for a clearer or more concise way, this is actually quite simple. I would avoid using "tuple" and "list" as variable names however, it is bad practice to name variables as their type.

Community
  • 1
  • 1
  • The linked question isn't really relevant; it's about splitting when you have multiple delimiters (so a single `str.split` call doesn't work), so you're choosing between regexes and doing one `str.split` followed by another split of one or more of the resulting strings. When it's "I want to split on a single fixed delimiter", the answer is `str.split`. – ShadowRanger Nov 12 '15 at 17:05
0

Well, the code that you have written is pretty concise but you could remove few more line by using the below code:

values = input("Enter some numbers:\n").split(",")
print(values) #This is the list
print(tuple(values)) #This is the tuple
Gourav Chawla
  • 470
  • 1
  • 4
  • 12