DaveL17
DaveL17

Reputation: 2015

Mutating a List in Python

I have a list of the form

['A', 'B', 'C', 'D']

which I want to mutate into:

[('Option1','A'), ('Option2','B'), ('Option3','C'), ('Option4','D')]

I can iterate over the original list and mutate successfully, but the closest that I can come to what I want is this:

["('Option1','A')", "('Option2','B')", "('Option3','C')", "('Option4','D')"]

I need the single quotes but don't want the double quotes around each list.

[EDIT] - here is the code that I used to generate the list; although I've tried many variations. Clearly, I've turned 'element' into a string--obviously, I'm not thinking about it the right way here.

array = ['A', 'B', 'C', 'D']
listOption = 0
finalArray = []

for a in array:
    listOption += 1
    element = "('Option" + str(listOption) + "','" + a + "')"
    finalArray.append(element)

Any help would be most appreciated.

[EDIT] - a question was asked (rightly) why I need it this way. The final array will be fed to an application (Indigo home control server) to populate a drop-down list in a config dialog.

Upvotes: 0

Views: 3038

Answers (3)

Adam Smith
Adam Smith

Reputation: 54183

[('Option{}'.format(i+1),item) for i,item in enumerate(['A','B','C','D'])]

# EDIT FOR PYTHON 2.5
[('Option%s' % (i+1), item) for i,item in enumerate(['A','B','C','D'])]

This is how I'd do it, but honestly I'd probably try not to do this and instead want to know why I NEEDED to do this. Any time you're making a variable with a number in it (or in this case a tuple with one element of data and one element naming the data BY NUMBER) think instead how you could organize your consuming code to not need that instead.

For instance: when I started coding professionally the company I work for had an issue with files not being purged on time at a few of our locations. Not all the files, mind you, just a few. In order to provide our software developer with the information to resolve the problem, we needed a list of files from which sites the purge process was failing on.

Because I was still wet behind the ears, instead of doing something SANE like making a dictionary with keys of the files and values of the sizes, I used locals() to create new variables WITH MEANING. Don't do this -- your variables should mean nothing to anyone but future coders. Basically I had a whole bunch of variables named "J_ITEM" and "J_INV" and etc with a value 25009 and etc, one for each file, then I grouped them all together with [item for item in locals() if item.startswith("J_")]. THAT'S INSANITY! Don't do this, build a saner data structure instead.

That said, I'm interested in how you put it all together. Do you mind sharing your code by editing your answer? Maybe we can work together on a better solution than this hackjob.

Upvotes: 4

Ofiris
Ofiris

Reputation: 6151

In addition to adsmith great answer, I would add the map way:

>>> map(lambda (index, item): ('Option{}'.format(index+1),item), enumerate(['a','b','c', 'd']))

[('Option1', 'a'), ('Option2', 'b'), ('Option3', 'c'), ('Option4', 'd')]

Upvotes: 1

ForgetfulFellow
ForgetfulFellow

Reputation: 2632

x = ['A','B','C','D']
option = 1
answer = []
for element in x:
  t = ('Option'+str(option),element) #Creating the tuple 
  answer.append(t)
  option+=1

print answer

A tuple is different from a string, in that a tuple is an immutable list. You define it by writing:

t = (something, something_else)

You probably defined t to be a string "(something, something_else)" which is indicated by the quotations surrounding the expression.

Upvotes: 3

Related Questions