0

I have two lists:

f= ['A', 'A']  
d = [(2096, 222, 2640), (1494, 479, 1285)]

I want a list!

LL = [('A', 2096, 222, 2640), ('A', 1494, 479, 1285)]

I am close with dic = zip(f,d)

but this give me this:

[('A', (2096, 222, 2640)), ('A', (1494, 479, 1285))]

How can i get LL?

5 Answers 5

4

Try:

LL = [ (x,) + y for x, y in zip(f, d) ]

This iterates through the convoluted arrays and adds the string outside the tuple to the tuple (by creating a new tuple, due to the fact tuples are immutable).

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

1 Comment

Note that it does not really add to the tuple, as tuples are immutable. It creates a new tuple.
1

the zip command does that along with dict:

>>>dict(zip([1,2,3],[1,2,3]))
{1:1,2:2,3:3}

2 Comments

@user428862: Then why did you tag the question with dictionary? ;) (and you name the variable dic btw which suggest to hold a dictionary, but this is me being nitpicking...)
I realized that dictionary refers to something I thought I wanted/needed. but its a list. "a: 34, 56, b: 565, 67" is not what I needed.
1

You can also do this with map() instead of zip()

LL = map(lambda x,y:(x,)+y, f, d)

(x,) is equivalent to tuple(x)

LL = map(lambda x,y:tuple(x)+y, f, d)

Comments

0
[(x,) + y for x,y in zip(f, d)]

Comments

0

In fact you have a list of strings and a list of tuples. Tuples are immutable, so you will have to reconstruct each tuple.

For only 2 items you can try:

[tuple(f[0])+d[0], tuple(f[1])+d[1]]

For N number of items look for "list comprehension", for example here: http://docs.python.org/tutorial/datastructures.html or build them up using a loop if that is more understandable.

Comments

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.