Conversion of List of Tuples

Discussion in 'Python' started by subhabangalore@gmail.com, Dec 3, 2012.

  1. Guest

    Dear Group,

    I have a tuple of list as,

    tup_list=[(1,2), (3,4)]
    Now if I want to covert as a simple list,

    list=[1,2,3,4]

    how may I do that?

    If any one can kindly suggest? Googling didn't help much.

    Regards,
    Subhabrata.
    , Dec 3, 2012
    #1
    1. Advertising

  2. John Gordon Guest

    In <> writes:

    > Dear Group,


    > I have a tuple of list as,


    > tup_list=[(1,2), (3,4)]
    > Now if I want to covert as a simple list,


    > list=[1,2,3,4]


    > how may I do that?


    new_list = []

    for t in tup_list:
    for item in t:
    new_list.append(item)

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Dec 3, 2012
    #2
    1. Advertising

  3. Gary Herron Guest

    On 12/03/2012 11:58 AM, wrote:
    > [(1,2), (3,4)]
    >>> L=[(1,2), (3,4)]
    >>>
    >>> [b for a in L for b in a]

    [1, 2, 3, 4]


    --
    Dr. Gary Herron
    Department of Computer Science
    DigiPen Institute of Technology
    (425) 895-4418
    Gary Herron, Dec 3, 2012
    #3
  4. Chris Kaynor Guest

    On Mon, Dec 3, 2012 at 11:58 AM, <> wrote:
    > Dear Group,
    >
    > I have a tuple of list as,
    >
    > tup_list=[(1,2), (3,4)]
    > Now if I want to covert as a simple list,
    >
    > list=[1,2,3,4]
    >
    > how may I do that?
    >
    > If any one can kindly suggest? Googling didn't help much.


    If you know they are always exactly two levels deep, you can use
    nested loops (in comprehension form):
    [item for tuple_ in list_ for item in tuple_]

    That could also be written how John recommended, in three lines.

    >
    > Regards,
    > Subhabrata.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    Chris Kaynor, Dec 3, 2012
    #4
  5. MRAB Guest

    On 2012-12-03 20:04, John Gordon wrote:
    > In <> writes:
    >
    >> Dear Group,

    >
    >> I have a tuple of list as,

    >
    >> tup_list=[(1,2), (3,4)]
    >> Now if I want to covert as a simple list,

    >
    >> list=[1,2,3,4]

    >
    >> how may I do that?

    >
    > new_list = []
    >
    > for t in tup_list:
    > for item in t:
    > new_list.append(item)
    >

    Or you could use .extend:

    new_list = []

    for t in tup_list:
    new_list.extend(t)
    MRAB, Dec 3, 2012
    #5
  6. On Tue, Dec 4, 2012 at 7:04 AM, John Gordon <> wrote:
    > In <> writes:
    >
    >> Dear Group,

    >
    >> I have a tuple of list as,

    >
    >> tup_list=[(1,2), (3,4)]
    >> Now if I want to covert as a simple list,

    >
    >> list=[1,2,3,4]

    >
    >> how may I do that?

    >
    > new_list = []
    >
    > for t in tup_list:
    > for item in t:
    > new_list.append(item)


    Which can be written more succintly as:

    new_list = []
    for t in tup_list:
    new_list.extend(t)

    In more general terms, what you're looking to do here is *flatten*
    your structure. Not sure if that would have helped in the web search
    that you doubtless did before asking this question. :)

    ChrisA
    Chris Angelico, Dec 3, 2012
    #6
  7. Guest

    On Tuesday, December 4, 2012 1:28:17 AM UTC+5:30, wrote:
    > Dear Group,
    >
    >
    >
    > I have a tuple of list as,
    >
    >
    >
    > tup_list=[(1,2), (3,4)]
    >
    > Now if I want to covert as a simple list,
    >
    >
    >
    > list=[1,2,3,4]
    >
    >
    >
    > how may I do that?
    >
    >
    >
    > If any one can kindly suggest? Googling didn't help much.
    >
    >
    >
    > Regards,
    >
    > Subhabrata.


    Thanks. But I am not getting the counter "5posts 0 views"...if moderator can please check the issue.
    , Dec 3, 2012
    #7
  8. John Gordon Guest

    In <> writes:

    > Thanks. But I am not getting the counter "5posts 0 views"...if
    > moderator can please check the issue.


    I logged in via Google Groups and all the replies were present. What
    is your question?

    (This group is not moderated.)

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Dec 3, 2012
    #8
  9. On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote:

    > Thanks. But I am not getting the counter "5posts 0 views"...if moderator
    > can please check the issue.


    What counter are you talking about?

    This is an email mailing list, also copied to the Usenet newsgroup
    comp.lang.python, and mirrored on other places including gmane and
    various web sites. Neither email nor Usenet include "counters", so you
    will have to explain what you are talking about.



    --
    Steven
    Steven D'Aprano, Dec 3, 2012
    #9
  10. Walter Hurry Guest

    On Mon, 03 Dec 2012 22:11:40 +0000, Steven D'Aprano wrote:

    > On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote:
    >
    >> Thanks. But I am not getting the counter "5posts 0 views"...if
    >> moderator can please check the issue.

    >
    > What counter are you talking about?
    >
    > This is an email mailing list, also copied to the Usenet newsgroup
    > comp.lang.python, and mirrored on other places including gmane and
    > various web sites. Neither email nor Usenet include "counters", so you
    > will have to explain what you are talking about.


    Doubtless he is talking about G**gle Groups, since I don't see his posts
    anyway.
    Walter Hurry, Dec 3, 2012
    #10
  11. Am 03.12.2012 20:58, schrieb :
    > Dear Group,
    >
    > I have a tuple of list as,
    >
    > tup_list=[(1,2), (3,4)]
    > Now if I want to covert as a simple list,
    >
    > list=[1,2,3,4]
    >
    > how may I do that?


    Another approach that has not yet been mentioned here:

    >>> a=[(1,2), (3,4)]
    >>> b=[]
    >>> map(b.extend, a)

    [None, None]
    >>> b

    [1, 2, 3, 4]

    map returns [None, None] because extend returns nothing, but now
    b==[1,2,3,4].

    There are more ways:

    >>> from operator import add
    >>> reduce(add, a)

    (1, 2, 3, 4)

    or

    >>> reduce(operator.add, (list(t) for t in a))

    [1, 2, 3, 4]

    I didn't do any performance testing, i guess the first one should be
    about as fast es the for-loop approach with .extend() and the other two
    might be quite slow. Although this only really matters if you have large
    lists.

    Greetings
    Alexander Blinne, Dec 4, 2012
    #11
  12. Hans Mulder Guest

    On 4/12/12 10:44:32, Alexander Blinne wrote:
    > Am 03.12.2012 20:58, schrieb :
    >> Dear Group,
    >>
    >> I have a tuple of list as,
    >>
    >> tup_list=[(1,2), (3,4)]
    >> Now if I want to covert as a simple list,
    >>
    >> list=[1,2,3,4]
    >>
    >> how may I do that?

    >
    > Another approach that has not yet been mentioned here:
    >
    >>>> a=[(1,2), (3,4)]
    >>>> b=[]
    >>>> map(b.extend, a)

    > [None, None]
    >>>> b

    > [1, 2, 3, 4]
    >
    > map returns [None, None] because extend returns nothing, but now
    > b==[1,2,3,4].


    It's considered bad style to use map it you don't want the list it
    produces.

    > There are more ways:
    >
    >>>> from operator import add
    >>>> reduce(add, a)

    > (1, 2, 3, 4)


    There's a built-in that does "reduce(operator.add"; it's called "sum":

    >>> sum(a, ())

    (1, 2, 3, 4)
    >>>

    > or
    >
    >>>> reduce(operator.add, (list(t) for t in a))

    > [1, 2, 3, 4]


    This is a valid use case for the map operator:

    >>> sum(map(list, a), [])

    [1, 2, 3, 4]
    >>>


    > I didn't do any performance testing, i guess the first one should be
    > about as fast as the for-loop approach with .extend() and the other two
    > might be quite slow. Although this only really matters if you have large
    > lists.


    Hope this helps,

    -- HansM
    Hans Mulder, Dec 4, 2012
    #12
  13. Neil Cerutti Guest

    On 2012-12-04, Hans Mulder <> wrote:
    > It's considered bad style to use map it you don't want the list it
    > produces.
    >
    >> There are more ways:
    >>
    >>>>> from operator import add
    >>>>> reduce(add, a)

    >> (1, 2, 3, 4)

    >
    > There's a built-in that does "reduce(operator.add"; it's called "sum":
    >
    >>>> sum(a, ())

    > (1, 2, 3, 4)


    I thought that sort of thing would cause a warning. Maybe it's
    only for lists.

    Here's the recipe from the itertools documentation:

    def flatten(listOfLists):
    "Flatten one level of nesting"
    return chain.from_iterable(listOfLists)

    --
    Neil Cerutti
    Neil Cerutti, Dec 4, 2012
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metiu uitem
    Replies:
    31
    Views:
    822
    Bengt Richter
    Nov 24, 2005
  2. Replies:
    5
    Views:
    552
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    572
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    721
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    228
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page