0

Say I have some lists:

t = ['a','b']
x = ['x1','x2','x3','x4']
y = ['y1','y2','y3','y4']

I would like to combine elements from those lists above, so I would have a new list looks like this

new = ['ax1y1x2y2','bx3y3x4y4']

How do I do this in Python?

7
  • 5
    What have you tried? Stack Overflow isn't a code-writing service. Commented Dec 24, 2021 at 23:12
  • Please show your efforts and format your strings correctly Commented Dec 24, 2021 at 23:13
  • Hi guys. I'm kind of new to programming and stack overflow. I'm not exactly sure what 's wrong with my string format. Commented Dec 24, 2021 at 23:16
  • Welcome to the community @user17758135. The 'y4' value didn't have the ' . Eitherway, you need to always provide whatever you were able to accomplish. Even if it was a for loop that didn't work, any code of a previous attempt is useful for us to tell you what went wrong on your solution and help you improve in your code. Commented Dec 24, 2021 at 23:24
  • If you have no idea how to do that you can investigate a bit further on how to compute the Cartesian product of a string. Which I think is what you might be looking for. Commented Dec 24, 2021 at 23:25

2 Answers 2

1

So your question is a bit vague which makes it hard for me to know exactly what you are looking for. But in general you can access the specific element n from a Python list foo with the code foo[n]. Therefore you can build a general list bar of length l where you also have lists ham, eggs and spam with something like:

bar = list()
for i in range(l):
    bar.append(str(ham[i]) + str(eggs[i]) + str(spam[i//2])) 

Where of course you can access whatever elements of ham, eggs, and spam are best for your purposes.

As a side note, the people in the comments on your post asking you to edit your string format mean that they want your code to be in the form:

t = ['a','b']

As opposed to:

y = ['y1,'y2','y3',y4']

Notice how the single quote ' to the right of y1 and to the left of y4 is missing in the text for y.

Hope this helps! If you ever have issues formatting your questions/answers in Stack Overflow see this webpage for assistance.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not quite sure if this is what you wanted, but I made a function that does what you described. There are many ways to make it more efficient, but I'll leave that up to you.

t = ['a','b']
x = ['x1','x2','x3','x4']
y = ['y1','y2','y3','y4']

def big_zip(t,x,y):
    a = zip(x, y)
    b = []
    new_list = []
    final = []
    for i in a:
        b.append(i[0]+i[1])
    for i in range(0,len(b)-1,2):
        new_list.append(b[i]+b[i+1])
    for i in range(0,len(t)):
        final.append(t[i]+new_list[i])
    return final

print(big_zip(t,x,y))

1 Comment

No problem! Anytime.

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.