Karma
Karma

Reputation: 269

Convert list to list of tuples with repeated value of item in each tuple

I have a few N-dimensional arrays as below. How can I convert these arrays into tuples?

num = ['1' '2']
letter1 = ['a' 'b' 'c' 'd' 'e' 'f']
letter2 = ['aa' 'bb' 'cc' 'dd' 'ee' 'ff']

The result I want is:

tuple1 = [(1,a),(1,b),(1,c),(1,d),(1,e),(1,f)]
tuple2 = [(2,aa),(2,bb),(2,cc),(2,dd),(2,ee),(2,ff)]

I have

tuple1 = tuple(num[0], letter1)
tuple2 = tuple(num[1], letter2)

But I got an error of TypeError: tuple() takes at most 1 argument (2 given)

What would be your approach? Any methods would be appreciated. Thanks.

Upvotes: 2

Views: 994

Answers (4)

Aaditya Ura
Aaditya Ura

Reputation: 12679

Already there are good answers, I just want to give you hint you can tweak your logic a little

num = ['1','2']
letter1 = ['a','b' ,'c', 'd', 'e', 'f']
letter2 = ['aa' ,'bb', 'cc', 'dd' ,'ee' ,'ff']
tuple1 = tuple(zip(*[[int(num[0])]*len(letter1),letter1]))
tuple2 = tuple(zip(*[[int(num[1])]*len(letter2), letter2]))

print(tuple1)
print(tuple2)

output:

((1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (1, 'e'), (1, 'f'))
((2, 'aa'), (2, 'bb'), (2, 'cc'), (2, 'dd'), (2, 'ee'), (2, 'ff'))

Upvotes: 1

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

You may use zip with itertools.repeat to achieve this as:

>>> from itertools import repeat
>>> num = ['1', '2']
>>> letter1 = ['a', 'b', 'c', 'd', 'e', 'f']

>>> tuple1 = list(zip(repeat(num[0]), letter1))
>>> tuple1
[('1', 'a'), ('1', 'b'), ('1', 'c'), ('1', 'd'), ('1', 'e'), ('1', 'f')]

PS: Note the commas , between elements in the above lists. Strings separated without comma are treated as a single string.


Inspired from the above answer, here's the list comprehension version of the solution to get tuple1 and tuple2 in a single go:

letter2 = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'] # rest of the variables same as the above solution

tuple1, tuple2 = [list(zip(repeat(a), b)) for a, b in zip(num, (letter1, letter2))]

where these variables will hold the below lists as values:

>>> tuple1
[('1', 'a'), ('1', 'b'), ('1', 'c'), ('1', 'd'), ('1', 'e'), ('1', 'f')]
>>> tuple2
[('2', 'aa'), ('2', 'bb'), ('2', 'cc'), ('2', 'dd'), ('2', 'ee'), ('2', 'ff')]

Upvotes: 7

Ajax1234
Ajax1234

Reputation: 71461

You can use iter and next:

num = ['1', '2']
num = iter(num)
letter1 = ['a', 'b', 'c', 'd', 'e', 'f']
letter2 = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff']
tuple1, tuple2 = [list(zip(i, [next(num)]*len(i))) for i in [letter1, letter2]]

Output:

[('a', '1'), ('b', '1'), ('c', '1'), ('d', '1'), ('e', '1'), ('f', '1')]
[('aa', '2'), ('bb', '2'), ('cc', '2'), ('dd', '2'), ('ee', '2'), ('ff', '2')]

Without iter:

tuple1, tuple2 = [[(i, h) for i in c] for c, h in zip([letter1, letter2], ['1', '2'])]

Upvotes: 2

sjw
sjw

Reputation: 6543

Some good answers already but I'll share an alternative using a list comprehension. It looks like a bit more code, but will work for larger lengths of nums and lists (as long as they are the same length as each other still):

letter1 = ['a', 'b', 'c', 'd', 'e', 'f']
letter2 = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff']
nums = [1, 2]

lists = [letter1, letter2]
tuples = [[(i, x) for x in l] for i, l in zip(nums, lists)]

tuple1, tuple2 = tuples

Results:

In [1]: tuple1
Out[1]: [(1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (1, 'e'), (1, 'f')]

In [2]: tuple2
Out[2]: [(2, 'aa'), (2, 'bb'), (2, 'cc'), (2, 'dd'), (2, 'ee'), (2, 'ff')]

Upvotes: 1

Related Questions