0

I have these lists:

sqvaluelist = []
valuelist = [(10.5,), (20.5,), (21.5,), (70.0,), (34.5,)]

I want to apply this code on the valuelist:

for value in valuelist:
    valuesquared = value*value
    sqvaluelist.append(valuesquared,)

but I received this error:

TypeError: can't multiply sequence by non-int of type 'tuple'

I think I know the reason behind this error, it is because every value is inside a separate tuple.

My question is, is there any way to take this values off their respective tuple, and just turn them into a list like

valuelist = [10.5, 20.5, 21.5, 70.0, 34.5]

without manually editing the structure of the existing list so that the for loop can be executed?

EDIT: I apologize! They are actually tuples! Added commas after each value. Sorry!

agf
  • 171,228
  • 44
  • 289
  • 238
DarsAE
  • 185
  • 3
  • 4
  • 12

6 Answers6

6

then just

import itertools
list(itertools.chain(*valuelist))
okm
  • 23,575
  • 5
  • 83
  • 90
  • 4
    nice, you could also use `itertools.chain.from_iterable(valuelist)` – Shep Apr 15 '12 at 07:06
  • +1 for the elegance. But on further reflection, I'm not sure this is the way to go: since `valuelist` is a list of **tuples**, it's likely that the _position_ of the values _in the tuple_ is important. As mentioned in [this post](http://stackoverflow.com/a/1708610/915501) tuples are expected to be heterogeneous. So, should the tuples acquire additional entries later on, chaining the values together would be inappropriate. – Shep Apr 15 '12 at 08:20
  • @Shep yes, I believe it's the reason that your answer is picked =) My answer is for demonstrating general Pythonic way to flatten a sequence containing sub-sequence items. – okm Apr 15 '12 at 08:30
6

To make

valuelist = [(10.5,), (20.5,), (21.5,), (70.0,), (34.5,)]

into

valuelist = [10.5, 20.5, 21.5, 70.0, 34.5]

I'd use list comprehension

valuelist = [x[0] for x in valuelist]
Shep
  • 7,990
  • 8
  • 49
  • 71
3
valuelist = [(10.5), (20.5), (21.5), (70.0), (34.5)]

is a list of ints:

>>> [(10.5), (20.5), (21.5), (70.0), (34.5)]
[10.5, 20.5, 21.5, 70.0, 34.5]

(10.5) is an integer. (10.5,) is a tuple of one integer.

Therefore:

>>> sqvaluelist = [x*x for x in valuelist]
>>> sqvaluelist
[110.25, 420.25, 462.25, 4900.0, 1190.25]
eumiro
  • 207,213
  • 34
  • 299
  • 261
1

Yes you can do so very easily in a one liner :

map(lambda x: x, valuelist)

This works because as @eumiro noted, (10.5) is actually a float and not a tuple. Tuple would be (10.5,).

To compute the squares it's as easy:

map(lambda x: x*x, valuelist)

If you have a list of real tuples like (10.5,), you can modify it like this:

map(lambda x: x[0], valuelist)
map(lambda x: x[0]*x[0], valuelist)
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
  • It's considered un-Pythonic to use `map` on a user-defined function instead of a list comprehension. – agf Apr 15 '12 at 07:12
  • @agf so where is the pythonic place to use maps? – Shep Apr 15 '12 at 07:59
  • 1
    @Shep this thread was informative on this topic: http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map – machine yearning Apr 15 '12 at 08:05
  • @Shep Yeah, that thread basically covers it. I occasionally use map when applying a single built in function to an iterable, because I know the performance is comparable or better, and it "feels" right to me because you don't have to give a name to each item in the list -- it emphasises you're applying the function to the whole list. – agf Apr 15 '12 at 18:48
1

Just access the first element of each tuple:

>>> valuelist = [(10.5,), (20.5,), (21.5,), (70.0,), (34.5,)]
>>> sqvaluelist = [x[0]*x[0] for x in valuelist]
>>> sqvaluelist
[110.25, 420.25, 462.25, 4900.0, 1190.25]
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

Doing it in pythonic way:

sqvaluelist = [v[0]**2 for v in valuelist]

Saurabh Singh
  • 287
  • 4
  • 9