user2242044
user2242044

Reputation: 9213

extending a list to a list

I am trying to extend values to a list if an element in List1 equals an item in List2. I am using extend instead of append because extend is suppose to merge two lists. Instead it seems to be nesting lists.

Removing the brackets around y[1],y[2] yields:

SyntaxError: Generator expression must be parenthesized if not sole argument

List1 = [['value1','value2','Value3'],['value4','value5','value6']]
List2 = [['option1','option2','value2'],['option3','option4','value5']]

for x in List1:
    x.extend([y[1],y[2]] for y in List2 if y[2] == x[1])
print List1

My output:

[['value1', 'value2', 'Value3', ['option2', 'value2']], ['value4', 'value5', 'value6', ['option4', 'value5']]]

Desired output:

[['value1', 'value2', 'Value3', 'option2', 'value2'], ['value4', 'value5', 'value6', 'option4', 'value5']]

Upvotes: 2

Views: 72

Answers (2)

Vivek Sable
Vivek Sable

Reputation: 10223

as [[y[1],y[2]] for y in List2 if y[2] == x[1]] return list of list

>>> List1 = [['value1','value2','Value3'],['value4','value5','value6']]
>>> List2 = [['option1','option2','value2'],['option3','option4','value5']]
>>> for x in List1:
...    [[y[1],y[2]] for y in List2 if y[2] == x[1]]
... 
[['option2', 'value2']]
[['option4', 'value5']]

so get first item from the list to extend in x

>>> List1 = [['value1','value2','Value3'],['value4','value5','value6']]
>>> List2 = [['option1','option2','value2'],['option3','option4','value5']]
>>> for x in List1:
...     x.extend([[y[1],y[2]] for y in List2 if y[2] == x[1]][0])
... 
>>> List1
[['value1', 'value2', 'Value3', 'option2', 'value2'], ['value4', 'value5', 'value6', 'option4', 'value5']]
>>> 

Python beginner Extended above code line by line.

  1. Used for loop to iterate items from the List1 and List2
  2. If condition to check values.
  3. Use list extend method to append list items into existing list.

code:

>>> List1 = [['value1','value2','Value3'],['value4','value5','value6']]
>>> List2 = [['option1','option2','value2'],['option3','option4','value5']]
>>> 
>>> for x in List1:
...     for y in List2:
...             if y[2]==x[1]:
...                     print "In if value:", [y[1],y[2]]
...                     x.extend([y[1],y[2]])
... 
In if value: ['option2', 'value2']
In if value: ['option4', 'value5']
>>> List1
[['value1', 'value2', 'Value3', 'option2', 'value2'], ['value4', 'value5', 'value6', 'option4', 'value5']]
>>> 

Upvotes: 1

Dan D.
Dan D.

Reputation: 74655

Use itertools.chain.from_iterable to flatten the iterator/list of iterators/lists to a iterator/list which then can be passed to .extend(. This will work in the case that:

([y[1],y[2]] for y in List2 if y[2] == x[1])

Has any number of matches. The example input only has one but it is better to expect more as reasonable. See:

>>> import itertools
>>> List1 = [['value1','value2','Value3'],['value4','value5','value6']]
>>> List2 = [['option1','option2','value2'],['option3','option4','value5']]
>>> 
>>> for x in List1:
...     x.extend(itertools.chain.from_iterable([y[1],y[2]] for y in List2 if y[2] == x[1]))
... 
>>> print List1
[['value1', 'value2', 'Value3', 'option2', 'value2'], ['value4', 'value5', 'value6', 'option4', 'value5']]

Upvotes: 1

Related Questions