Skip to main content
  1. About
  2. For Teams
Asked
Viewed 154 times
0

I have 2 lists :

list1 contains "T" "F" "T" "T"

list2 contains "a" "b" "c" "d"

I want to create a third list such that I append element1 in list1 to element1 in list2.

So list3 would be the following: "Ta" "Fb" "Tc" "Td"

How can i do that?

1
  • 3
    We all wait in suspense for the next problem from @hssss' homework set.
    Zeke
    –  Zeke
    2010-12-05 07:54:13 +00:00
    Commented Dec 5, 2010 at 7:54

3 Answers 3

4

Use zip: [x + y for x, y in zip(list1, list2)].

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

Comments

1

zip, as others have suggested, is good. izip, I would suggest, is better for longer lists.

>>> from itertools import izip
>>> list3 = [x+y for x,y in izip(list1, list2)]
>>> list3
['Ta', 'Fb', 'Tc', 'Td']

See also the documentation on list comprehensions, they're an essential tool in Python programming.

Comments

0

Your lists

>>> t = ["T", "F", "T", "T"]
>>> t1 = ["a", "b", "c", "d"]

Group them using zip function:

>>> t2 = zip(t, t1)
>>> t2
[('T', 'a'), ('F', 'b'), ('T', 'c'), ('T', 'd')]

You could now manipulate the list for desired result:

>>> ["".join(x) for x in t2]
['Ta', 'Fb', 'Tc', 'Td']
>>> 

2 Comments

Perhaps having two ts isn't the best way to demonstrate this functionality.
@Johnsyweb: Yes I agree that can be confusing.

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.