Ashraful Islam Joy
Ashraful Islam Joy

Reputation: 51

How to remove blank inverted comma from my list and tuple in python?

I am a new python developer. I will be glad if you help me with this. The problem is I made a list and tuple. A user has to type some comma-separated numbers. So I was expecting this result :

List :  ['3', '4', '2', '3', '5']
Tuple : ('3', '4', '2', '3', '5')

and also the number should show in ascending order. It worked by the below code but shows some extra inverted comma so How to remove those blank commas.

Type some comma-separated numbers: 4 4 5 6 2 3 1

List  :  [' ', ' ', ' ', ' ', ' ', ' ', '1', '2', '3', '4', '4', '5', '6']      
Tuple :  [' ', ' ', ' ', ' ', ' ', ' ', '1', '2', '3', '4', '4', '5', '6'] 

This is the code I am using to make the list and tuple in ascending order. Another thing is when I give the int() method in the input so that the user only can type the number, not any string. It shows an error so how to do that?

values = input("Type some comma separated numbers: ")
list = values.split()
tuple = tuple(list)
tuple= sorted(values, reverse = False)
list= sorted(values, reverse = False)
print('List  : ', list)
print('Tuple : ', tuple)

Upvotes: 0

Views: 407

Answers (4)

Arehandoro
Arehandoro

Reputation: 387

You can't use int() when calling input() because by separating the numbers with a space, you're creating a string with numbers that do not resemble to a base10 number, hence the ValueError you get.

For example:

values = '4 4 5 6 2 3 1'
int(values)
ValueError: invalid literal for int() with base 10: '4 4 5 6 2 3 1'

However:

values = '4456231'
int(values)
4456231

As mentioned by @Corralien, you can't use built-in reserved keywords like list and tuple (unless you delete their namespace first, but that would make things unnecessarily complex). It's also considered good syntax to don't separate the = sign for keywords with spaces. To get what you need, you could do:

values = input("Type some comma separated numbers: ")
l = sorted(values.split(), reverse=False)
t = sorted(tuple(l), reverse=False)
print('List  : ', l)
print('Tuple : ', t)

But that wouldn't get you the values as integers, in which case the best would be doing list comprehension. We use it to iterate between the values of the list l, and convert them to int in the list n.

values = input("Type some comma separated numbers: ")
l = sorted(values.split(), reverse=False)
n = [int(x) for x in l]
t = sorted(tuple(n), reverse=False)
print('List  : ', n)
print('Tuple : ', t)

However, you're asking the user to enter values that are comma separated, not spaces. Therefore, most users would enter 4,4,5,6,2,3,1 rather 4 4 5 6 2 3 1. In that case, you'd need to adapt your code to look more like this:

values = input("Type some comma separated numbers: ")
l = sorted(values.split(','), reverse=False)
n = [int(x) for x in l]
t = sorted(tuple(n), reverse=False)
print('List  : ', n)
print('Tuple : ', t)

Upvotes: 1

Patrick Artner
Patrick Artner

Reputation: 51643

You messed your variable names up: your code stores the input() into values.

Then you split values and store the splitted data it into list (do not use built in names as variables!).

Then you do tuple= sorted(values, reverse = False) which uses the unsplit (!) original string - not the splitted data you just created.

THAT is your mistake you need to fix.

Essentially you sort the unsplit string wich gives you a sorted list of characters of your inputs!

After that commit to one type of splitting - by spaces, by commas or whatever and stick to it.

Upvotes: 0

gautham
gautham

Reputation: 88

  1. Don't user list, tuple, set or any reserved keywords as variable names.
  2. Use values.split(',') instead of values.split(), you must pass the separator that will be used for splitting your string.
  3. For sorting a list sorted(<your_list>) or <your_list>.sort() can be used - If you are curious to know the difference between these two

Finally your code can be minimized to,

values         = input("Type some comma separated numbers: ")
list_variable  = sorted(values.split(','))
tuple_variable = tuple(list_variable)
print('List  : ', list_variable)
print('Tuple : ', tuple_variable)

Upvotes: 1

Bibhav
Bibhav

Reputation: 1757

You can simply do this:

values = input("Type some comma separated numbers: ")
List=sorted(values.split(','))
Tuple=tuple(List)
print('List  : ', List)
print('Tuple : ', Tuple)

Upvotes: 0

Related Questions