0

I everyone,

I struggle to convert an array of array like this :

[
    ["foo","bar","foo","bar","foo"],
    ["foo","bar","foo","bar","foo"],
    ["foo","bar","foo","bar","foo"],
    ["foo","bar","foo","bar","foo"]
]

to an array of tuples (array, str) like this:

[
    (["foo","bar","foo","bar","foo"], "type1"), 
    (["foo","bar","foo","bar","foo"], "type1"), 
    (["foo","bar","foo","bar","foo"], "type1"), 
    (["foo","bar","foo","bar","foo"], "type1")
]

I did find a way to append the type to the array but it's not exactly what I want:

[
    ["foo","bar","foo","bar","foo", "type1"], 
    ["foo","bar","foo","bar","foo", "type1"], 
    ["foo","bar","foo","bar","foo", "type1"], 
    ["foo","bar","foo","bar","foo", "type1"]
]

Do you have something better ? Thanks :)

2
  • you always need "type1" in array ? Commented Jan 29, 2021 at 10:07
  • how are the types determined in association with the list of lists? what determined type1 type2 ect with the list Commented Jan 30, 2021 at 14:15

3 Answers 3

2

Just initialize a tuple for every element in the list:

l = [
    ["foo","bar","foo","bar","foo"],
    ["foo","bar","foo","bar","foo"],
    ["foo","bar","foo","bar","foo"],
    ["foo","bar","foo","bar","foo"]]

new_l = [(i, "type1") for i in l]
print(new_l)

> [
    (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'),
    (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'),
    (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'),
    (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1')
]
Sign up to request clarification or add additional context in comments.

Comments

1

Solution

Shortest solution: list(zip(vals, types)) 🔥🔥🔥

vals = [
    ["foo","bar","foo","bar","foo"], 
    ["foo","bar","foo","bar","foo"], 
    ["foo","bar","foo","bar","foo"], 
    ["foo","bar","foo","bar","foo"]
]

# If you must specify different types for each element
# uncomment the following line
# types = ['type1', 'type2', 'type3', 'type4']

# If all of them should have the same type
types = ['type1' for _ in range(len(vals))]

# Finally combine vals and types
list(zip(vals, types))

Output:

[(['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'),
 (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'),
 (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'),
 (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1')]

4 Comments

@camile-saury Please try this and let me know if you have any questions.
1. You can create list with same elements much simpler: types = ['type1'] * len(vals). 2. You can use cycle() or repeat() which won't create long list in memory and will make your expression even shorter: list(zip(vals, cycle(('type1',)))) OR list(zip(vals, repeat('type1', len(vals)))) 3. Your solution is not shortest !!!
type1 was the only type used. how did you determine to use type1
@CamilleSAURY Please also consider voting-up, given you found the answer useful. Thank you.
1

You may use a list comprehension, and for each build the tuple

vals = [["foo", "bar", "foo", "bar", "foo"], ["foo", "bar", "foo", "bar", "foo"],
        ["foo", "bar", "foo", "bar", "foo"], ["foo", "bar", "foo", "bar", "foo"]]

result = [(i, "type1") for i in vals]
print(result)

# [(["foo", "bar", "foo", "bar", "foo"], "type1"), (["foo", "bar", "foo", "bar", "foo"], "type1"),
   (["foo", "bar", "foo", "bar", "foo"], "type1"), (["foo", "bar", "foo", "bar", "foo"], "type1")]

This is equivalent, with a for-loop, to

result = []
for sublist in vals:
    result.append((sublist, "type1"))

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.