Clay Finney
Clay Finney

Reputation: 27

Adding Specific Value to Nested Lists

I'm having a comprehension issue on a specific problem. I have a preexisting nested list, and I want to match and append one value from a different list to the end of each nested list. A quick example of what I've tried, but where I'm stuck:

 initial_values = [["First", 1], ["Second", 2], ["Third", 3], ["Fourth", 4]]
other_values = [1,2,3,4]

for sublist in initial_values:
    for i in other_values:
        sublist.append(i)


print initial_values

This returns [['First', 1, 1, 2, 3, 4], ['Second', 2, 1, 2, 3, 4], ['Third', 3, 1, 2, 3, 4], ['Fourth', 4, 1, 2, 3, 4]]

I want it it to ideally return [['First', 1, 1], ['Second', 2, 2], ['Third', 3, 3], ['Fourth', 4, 4]]

Upvotes: 1

Views: 58

Answers (6)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

One of the alternative to achieve it using map() as:

>>> map(lambda x: x[0] + [x[1]], zip(initial_values, other_values))
[['First', 1, 1], ['Second', 2, 2], ['Third', 3, 3], ['Fourth', 4, 4]]

Upvotes: 0

WLatif
WLatif

Reputation: 1378

If you want to use for loop you can try

initial_values = [["First", 1], ["Second", 2], ["Third", 3], ["Fourth", 4]]
other_values = [1,2,3,4]

for i in range(0,len(other_values)):
    initial_values[i].append(other_values[i])

print initial_values

output:
[['First', 1, 1], ['Second', 2, 2], ['Third', 3, 3], ['Fourth', 4, 4]]

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 113915

Your double for-loop takes each sublist in turn (the outer for-loop) and appends every element of other_values to it (the inner for-loop). What you want instead is to add each element of other_values to the corresponding sublist (i.e. the sublist at the same position/index). Therefore, what you need is only one for-loop:

initial_values = [["First", 1], ["Second", 2], ["Third", 3], ["Fourth", 4]]
other_values = [1,2,3,4]

for i in range(len(initial_values)):  # get all the valid indices of `initial_values`
    initial_values[i].append(other_values[i])

Here's are some simpler ways to do it:

for i,subl in enumerate(initial_values):
    subl.append(other_values[i]))

Or

for subl, val in zip(initial_values, other_values):
    subl.append(val)

Upvotes: 1

zvone
zvone

Reputation: 19352

Built-in function zip will match first item in first list with first item in second list etc.

initial_values = [["First", 1], ["Second", 2], ["Third", 3], ["Fourth", 4]]
other_values = [1,2,3,4]

for initials, other in zip(initial_values, other_values):
    initials.append(other)

Upvotes: 0

Mureinik
Mureinik

Reputation: 311163

You can use zip to match elements of the same index from different lists. From there on, you're a simple list concatenation away:

[a + [b] for a,b in zip(initial_values, other_values)]

Upvotes: 2

Blue Star
Blue Star

Reputation: 1952

It seems like you want to go through both lists with a single iteration. You could achieve that using zip:

for sublist, i in zip(initial_values, other_values):
    sublist.append(i)

Upvotes: 1

Related Questions