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

Iain King iainking at gmail.com
Thu Jun 5 16:30:21 CEST 2008


On Jun 5, 1:41 pm, Jeff Nyman <jeffny... at gmail.com> 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



More information about the Python-list mailing list