drbunsen
drbunsen

Reputation: 10679

Help with understanding list function

When I type the following into the interpreter I get the desired output behavior:

>>> x = (7, 2, 1, 1, 6, 2, 1, 2, 1, 2, 2, 6)
>>> y = list(x)
>>> y
[7, 2, 1, 1, 6, 2, 1, 2, 1, 2, 2, 6]

Above, I simply converted a tuple to a list. However, when I run the following code I get an answer that I don't understand.

pwm = input("enter PWM: ")
npwm = pwm.replace('),(', ', ')
y = list(npwm)
print(y)

Output:

['(', '7', ',', ' ', '2', ',', ' ', '1', ',', ' ', '1', ',', ' ', '6', ',', ' ', '2', ',', ' ', '1', ',', ' ', '2', ',', ' ', '1', ',', ' ', '2', ',', ' ', '2', ',', ' ', '6', ')']

Can anyone explain to me what is happening? Why does the above code not product the desired output of:

[7, 2, 1, 1, 6, 2, 1, 2, 1, 2, 2, 6]

EDIT: Wow, I can't thank everyone enough for the help! I greatly appreciate everyone's patients and willingness to help with my beginner questions. Thank you very much. Below is the solution that I got to worked:

pwm = (7, 2, 1, 1),(6, 2, 1, 2),(1, 2, 2, 6)  
npwm = pwm.replace('),(',', ').strip('(').strip(')')  
y = list(ast.literal_eval(npwm))  
print(y)  

Upvotes: 0

Views: 125

Answers (5)

unutbu
unutbu

Reputation: 879231

A safe (more restrictive) alternative to using eval is ast.literal_eval:

import ast
pwm = raw_input("enter PWM: ")
y = list(ast.literal_eval(pwm))
print(y)

Running

% test.py
enter PWM: (7), (2), (1), (1), (6), (2), (1), (2), (1), (2), (2), (6)

yields

[7, 2, 1, 1, 6, 2, 1, 2, 1, 2, 2, 6]

Note doing it this way avoids the fragile (and hard-to-read) replace('),(',','), which only works when there is no space after the comma. E.g. (7),(2) instead of (7), (2).

Upvotes: 3

senderle
senderle

Reputation: 150957

When you call list on a string, it generates a list in which each character is an item in the list. So for example:

>>> list("hey, you")
['h', 'e', 'y', ',', ' ', 'y', 'o', 'u']

Since the data returned from input in this case is a string, calling list on it generates a list of characters.

For the sake of demonstration, here's a basic way to convert the string '(1, 2, 3)' into the list [1, 2, 3]:

>>> pwm = raw_input("enter PWM: ")
enter PWM: (1, 2, 3)
>>> pwm
'(1, 2, 3)'
>>> npwm = pwm.strip('(').strip(')')
>>> npwm
'1, 2, 3'
>>> npwm = npwm.split(',')
>>> npwm
['1', ' 2', ' 3']
>>> y = [int(x) for x in npwm]
>>> y
[1, 2, 3]

Some people have suggested using eval on the string returned by input. Although that would do what you want, it's a very bad habit to get into, because python will then evaluate whatever expression the (possibly malicious) user decides to enter.

Also, as Sven Marnach correctly pointed out, input does what you expect in python 2.x. Fortunately that's been corrected in 3.0, which you must be using.

Upvotes: 6

Daniel Lubarov
Daniel Lubarov

Reputation: 7924

I think you want

y = list(eval(npwn))

Upvotes: 0

Luka Rahne
Luka Rahne

Reputation: 10447

You are making string from string and then you convert string to list. What are you looking as final result is list from string.

Result you posted in beginning is not string but a presentation of list. It is list.

Upvotes: 0

Achim
Achim

Reputation: 15692

You don't get the difference between data and source code. pwm will hold the string you have typed in, not a tuple. If you put a string into the list function, you will get back a list with the single characters of the string. That's what you get in your output.

Upvotes: 0

Related Questions