Creating A Tuple From A List, Adding To Tuple As You Do

Discussion in 'Python' started by Jeff Nyman, Jun 5, 2008.

  1. Jeff Nyman

    Jeff Nyman Guest

    Greetings all.

    The subject line of this thread is probably one of the worst ever. I
    was trying to encapsulate what I am doing. Based on my new-found
    knowledge from another thread, I'm able to get a list of directories
    and they come to me in the form of a list. Here is an example:

    from glob import glob
    DC_List = glob('\\\\vcdcflx006\\Flex\\Sites\\*\\')
    DC_List = ['Baltimore', 'Birmingham', 'Cincinnati', 'Cleveland',
    LosAngeles']

    (Each element in the DC_List is actually a full directory path, but I
    shortened that in the interest of clarity.)

    The problem is that I need to pass this list to a list control in a
    wxWidgets application. In order to do that, I need to pass in a list
    like this:

    [ ('Baltimore', ''), ('Birmingham', ''), ('Cincinnati', ''),
    ('Cleveland', ''), ('LosAngeles', '') ]

    In other words, each element in the list is a tuple that has an empty
    second string. The problem I'm having is in converting my list above
    to be of this type. I can't do append because that (logically) puts
    everything at the end. I did try this:


    for count in range(0, len(DC_List)):
    DC_List.insert(count, '')


    Here I was thinking I could insert a '' into the right place after
    each entry in the list. That doesn't quite work. Does anyone have an
    idea of a good approach here? (I did search on tuples and lists and
    while I found a lot of information about both, I couldn't find a
    solution that did what I'm discussing above.)

    - Jeff
    Jeff Nyman, Jun 5, 2008
    #1
    1. Advertising

  2. Jeff Nyman <> writes:
    > from glob import glob
    > DC_List = glob('\\\\vcdcflx006\\Flex\\Sites\\*\\')
    > DC_List = ['Baltimore', 'Birmingham', 'Cincinnati', 'Cleveland',
    > LosAngeles']


    > The problem is that I need to pass this list to a list control in a
    > wxWidgets application. In order to do that, I need to pass in a list
    > like this:
    >
    > [ ('Baltimore', ''), ('Birmingham', ''), ('Cincinnati', ''),
    > ('Cleveland', ''), ('LosAngeles', '') ]


    That's not hard:

    [ (x,'') for x in DC_List ]

    Yours
    Karsten
    Karsten Heymann, Jun 5, 2008
    #2
    1. Advertising

  3. Jeff Nyman

    Paul McGuire Guest

    On Jun 5, 7:41 am, Jeff Nyman <> wrote:
    > DC_List = ['Baltimore', 'Birmingham', 'Cincinnati', 'Cleveland',
    > LosAngeles']
    >
    > (Each element in the DC_List is actually a full directory path, but I
    > shortened that in the interest of clarity.)
    >
    > The problem is that I need to pass this list to a list control in a
    > wxWidgets application. In order to do that, I need to pass in a list
    > like this:
    >
    > [ ('Baltimore', ''), ('Birmingham', ''), ('Cincinnati', ''),
    > ('Cleveland', ''), ('LosAngeles', '') ]
    >


    tupleized_city_list = [ (city,'') for city in DC_list ]

    -- Paul
    Paul McGuire, Jun 5, 2008
    #3
  4. Jeff Nyman

    Fuzzyman Guest

    On Jun 5, 1:41 pm, Jeff Nyman <> wrote:
    > Greetings all.
    >
    > The subject line of this thread is probably one of the worst ever. I
    > was trying to encapsulate what I am doing. Based on my new-found
    > knowledge from another thread, I'm able to get a list of directories
    > and they come to me in the form of a list. Here is an example:
    >
    > from glob import glob
    > DC_List = glob('\\\\vcdcflx006\\Flex\\Sites\\*\\')
    > DC_List = ['Baltimore', 'Birmingham', 'Cincinnati', 'Cleveland',
    > LosAngeles']
    >




    NEW_LIST = [(entry, '') for entry in DC_List]

    Does this get you what you want?

    Michael Foord
    http://www.ironpythoninaction.com

    > (Each element in the DC_List is actually a full directory path, but I
    > shortened that in the interest of clarity.)
    >
    > The problem is that I need to pass this list to a list control in a
    > wxWidgets application. In order to do that, I need to pass in a list
    > like this:
    >
    > [ ('Baltimore', ''), ('Birmingham', ''), ('Cincinnati', ''),
    > ('Cleveland', ''), ('LosAngeles', '') ]
    >
    > In other words, each element in the list is a tuple that has an empty
    > second string. The problem I'm having is in converting my list above
    > to be of this type. I can't do append because that (logically) puts
    > everything at the end. I did try this:
    >
    > for count in range(0, len(DC_List)):
    > DC_List.insert(count, '')
    >
    > Here I was thinking I could insert a '' into the right place after
    > each entry in the list. That doesn't quite work. Does anyone have an
    > idea of a good approach here? (I did search on tuples and lists and
    > while I found a lot of information about both, I couldn't find a
    > solution that did what I'm discussing above.)
    >
    > - Jeff
    Fuzzyman, Jun 5, 2008
    #4
  5. Jeff Nyman

    Jeff Nyman Guest

    Thanks to everyone who responded!

    Yes, those solutions all work and do what I need. I'm also getting
    much more familiar with how flexible Python is in terms of its
    language. I think that's been the hardest challenge for me. I'm
    usually finding I try to "overdo it" when coming up with solutions.

    Once again, many thanks.

    - Jeff
    Jeff Nyman, Jun 5, 2008
    #5
  6. Jeff Nyman

    John Salerno Guest

    "Jeff Nyman" <> wrote in message
    news:...

    > Does anyone have an
    > idea of a good approach here?


    I think it's pretty cool that all three responses to your question suggested
    the exact same solution. But I guess that in itself is a feature of Python.
    :)
    John Salerno, Jun 5, 2008
    #6
  7. Hi Jeff,

    Jeff Nyman <> writes:
    > I did try this:
    >
    > for count in range(0, len(DC_List)):
    > DC_List.insert(count, '')


    On additional note: You can be quite sure you'll never have to iterate
    over the length of a list (or tuple) in python. Just iterate over the
    list itself:

    for DC in DC_List:
    # do something with DC.

    Yours
    Karsten
    Karsten Heymann, Jun 5, 2008
    #7
  8. Jeff Nyman

    Iain King Guest

    On Jun 5, 1:41 pm, Jeff Nyman <> wrote:
    > Greetings all.
    >
    > The subject line of this thread is probably one of the worst ever. I
    > was trying to encapsulate what I am doing. Based on my new-found
    > knowledge from another thread, I'm able to get a list of directories
    > and they come to me in the form of a list. Here is an example:
    >
    > from glob import glob
    > DC_List = glob('\\\\vcdcflx006\\Flex\\Sites\\*\\')
    > DC_List = ['Baltimore', 'Birmingham', 'Cincinnati', 'Cleveland',
    > LosAngeles']
    >
    > (Each element in the DC_List is actually a full directory path, but I
    > shortened that in the interest of clarity.)
    >
    > The problem is that I need to pass this list to a list control in a
    > wxWidgets application. In order to do that, I need to pass in a list
    > like this:
    >
    > [ ('Baltimore', ''), ('Birmingham', ''), ('Cincinnati', ''),
    > ('Cleveland', ''), ('LosAngeles', '') ]
    >
    > In other words, each element in the list is a tuple that has an empty
    > second string. The problem I'm having is in converting my list above
    > to be of this type. I can't do append because that (logically) puts
    > everything at the end. I did try this:
    >
    > for count in range(0, len(DC_List)):
    > DC_List.insert(count, '')
    >
    > Here I was thinking I could insert a '' into the right place after
    > each entry in the list. That doesn't quite work. Does anyone have an
    > idea of a good approach here? (I did search on tuples and lists and
    > while I found a lot of information about both, I couldn't find a
    > solution that did what I'm discussing above.)
    >
    > - Jeff


    I know a ton of people have already replied with list comprehensions,
    but I figured I'd chime in with one that also strips out the path of
    your folders for you (since I'm not sure how you are managing that
    just now)

    cities = [(os.path.basename(x), '') for x in glob('\\\\vcdcflx006\\Flex
    \\Sites\\*\\')]

    I tend to use / instead of \\ as a folder seperator, it should work
    for you (I think):
    cities = [(os.path.basename(x), '') for x in glob('//vcdcflx006/Flex/
    Sites/*')]

    Iain
    Iain King, Jun 5, 2008
    #8
  9. Jeff Nyman

    Terry Reedy Guest

    "Karsten Heymann" <> wrote in message
    news:-cable.net...
    | Hi Jeff,
    |
    | Jeff Nyman <> writes:
    | > I did try this:
    | >
    | > for count in range(0, len(DC_List)):
    | > DC_List.insert(count, '')
    |
    | On additional note: You can be quite sure you'll never have to iterate
    | over the length of a list (or tuple) in python. Just iterate over the
    | list itself:
    |
    | for DC in DC_List:
    | # do something with DC.

    Unless you want to modify the list in place.

    for i in range(len(cities)):
    cities = (cities, '')

    #or perhaps better

    for i,city in enumerate(cities):
    cities = (city,'')

    tjr
    Terry Reedy, Jun 5, 2008
    #9
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    803
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    947
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    610
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    817
    Antoon Pardon
    Mar 30, 2005
  5. Davy
    Replies:
    3
    Views:
    1,885
    Wildemar Wildenburger
    Nov 7, 2007
Loading...

Share This Page