1

I want to join the elements of two lists into one list and add some characters, like so:

list_1 = ['some1','some2','some3']
list_2 = ['thing1','thing2','thing3']

joined_list = ['some1_thing1', 'some2_thing2', 'some3_thing3']

however i don't know in advance how many lists I will have to do this for, i.e. I want to do this for an arbitrary number of lists

Also, I currently receive a list in the following form:

list_A = [('some1','thing1'),('some2','thing2'),('some3','thing3')]

so I split it up into lists like so:

list_B = [i for i in zip(*list_A)]

I do this because sometimes I have an int instead of a string

list_A = [('some1','thing1',32),('some1','thing1',42),('some2','thing3', 52)] 

so I can do this after

list_C = [list(map(str,list_B[i])) for i in range(0,len(list_B)]

and basically list_1 and list_2 are the elements of list_C.

So is there a more efficient way to do all this ?

3 Answers 3

3

Try this if you are using python>=3.6:

[f'{i}_{j}' for i,j in zip(list_1, list_2)]

If you using python3.5, you can do this:

 ['{}_{}'.format(i,j) for i,j in zip(list_1, list_2)]

also you can use this if you don't want to use formatted string:

['_'.join([i,j]) for i,j in zip(list_1, list_2)]
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to make this apply for an arbitrary number of lists ?
@AnarKi yes, the below answer, but it was suppose to be over the zip object, not list_A.
@juanpa.arrivillaga below might change with votes or whatnot.
@AnarKi what do you mean by arbitrary number of lists? you give an example?
2

You can join function like this on the base list_A, itself, no need to split it for probable int values:

list_A = [('some1','thing1',32),('some1','thing1',42), ('some2','thing3', 52)] 
["_".join(map(str, i)) for i in list_A]

Output:

['some1_thing1_32', 'some1_thing1_42', 'some2_thing3_52']

Update:

For you requirement, where you want to ignore last element for last tuple in your list_A, need to add if-else condition inside the list-comprehension as below:

["_".join(map(str, i)) if list_A.index(i) != len(list_A)-1 else "_".join(map(str, i[:-1])) for i in list_A  ]

Updated Output:

['some1_thing1_32', 'some1_thing1_42', 'some2_thing3']

10 Comments

i already tried this. This will join the elements themselves which is not what I want, i.e. some1_some2_some3 etc.
@AnarKi, I added the output, is this not same as what you mentioned in joined_list?
This works fine, I actually tried on list_B that's why it didn't work... sorry about that
One more thing, do you know if the last element of list_A can be omitted sometimes ? like list_A[:-1],m resulting in this: ['some1_thing1', 'some1_thing1, 'some2_thing3'] without 32,42..
Thanks, in that case, would you like to upvote and accept?
|
1

For ignoring the last element of every tuple in list_A, I found this to be the quickest way:

["_".join(map(str, i)) for i in [x[:-1] for x in list_A] ]

2 Comments

this will ignore third element for all cases. is that expected?
yes that is what I wanted, basically exclude the last element of each tuple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.