-3

I would like to change

mylist = [['1'],['2'],['3']]

to be

mynewlist = (1,2,3)

How to do that in python ?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
JPC
  • 5,063
  • 20
  • 71
  • 100
  • 5
    I'm not sure if you are aware that `(1,2,3)` is a tuple and not a list. Which specifically do you want to end up with? The tuple `(1,2,3)`, or the list `[1,2,3]`? – Magnus Hoff Sep 28 '12 at 18:20
  • 1
    possible duplicate of [Flattening a shallow list in Python](http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python) and [Converting a list of strings to ints (or doubles) in Python](http://stackoverflow.com/questions/11722882/converting-a-list-of-strings-to-ints-or-doubles-in-python) and many others. – Felix Kling Sep 28 '12 at 18:21
  • hmm.. my goal is to put "mynewlist" into a odbc connection --> cursor.execute("select x from table where list in (?),(mynewlist)) – JPC Sep 28 '12 at 20:41

1 Answers1

7

A simple list comprehension will do:

mynewlist = [int(x[0]) for x in mylist]

Of course, if you actually want a tuple as output:

mynewtuple = tuple(int(x[0]) for x in mylist)
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Hi mgilson, I'm trying to get csv to use in a where statement using odbc within python something like " where id in (list) " .. if that explains ... – JPC Sep 28 '12 at 20:49