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

J

Jeff Nyman

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
 
K

Karsten Heymann

Jeff Nyman said:
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
 
P

Paul McGuire

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
 
F

Fuzzyman

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
 
J

Jeff Nyman

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
 
J

John Salerno

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.
:)
 
K

Karsten Heymann

Hi Jeff,

Jeff Nyman said:
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
 
I

Iain King

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
 
T

Terry Reedy

| Hi Jeff,
|
| > 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top