estebanpdl
estebanpdl

Reputation: 1233

Append items from Y list to one specific list from X list Python

I have two lists x and y:

x list have several lists inside. For example:

x = [['1', 'hello', 'a'], ['3', 'hello', 'b'], ['11', 'hello', 'c'], ['2', 'hello', 'd'], ['4', 'hello', 'e'], ['22', 'hello', 'f']]

y list is a single list. The items inside are urls which contain the information from each list within x list.

y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']

I got the information from web scraping. So I'm trying to append one single item from y list to one single list from x list. For example, y[0] item should be appended to x[0] list, but y[1] item should be appended to x[3].

This is the output I'm looking for:

output = [['1', 'hello', 'a', 'odd1'], ['3', 'hello', 'b', 'odd3'], ['11', 'hello', 'c', 'odd11'], ['2', 'hello', 'd', 'even2'], ['4', 'hello', 'e', 'even4'], ['22', 'hello', 'f', 'even22']]

I don't know how to code this information. First, the items from y list are sorted, but the lists from x list are not. However, they have a pettern. They start with the information from odd rows I scraped, and then, the information from even rows.

I have tried with these in order to sort x list in the first place:

1. x.sort(key=int)
2. sorted(x) | Result [['1...'], ['11...], ['2...'], ['22...'], ['3...]...]
3. x = [int(x) for x in x]

How ever I don´t a good result.

For the other hand, I have tried to append the items from y to x lists in this simple way:

for i in x:
    i.append(y[:])

Obviusly, all items from y list were appended in each list from x

How can I solve this code. Thanks!

Upvotes: 0

Views: 183

Answers (3)

unutbu
unutbu

Reputation: 879591

Reorder y with

In [6]: y[::2]+y[1::2]
Out[6]: ['odd1', 'odd3', 'odd11', 'even2', 'even4', 'even22']

Then you can pair the reordered ys with x using zip:

x = [['1', 'hello', 'a'], ['3', 'hello', 'b'], ['11', 'hello', 'c'], 
     ['2', 'hello', 'd'], ['4', 'hello', 'e'], ['22', 'hello', 'f']]
y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']
print([xi+[yi] for xi, yi in zip(x, y[::2]+y[1::2])])

yields

[['1', 'hello', 'a', 'odd1'],
 ['3', 'hello', 'b', 'odd3'],
 ['11', 'hello', 'c', 'odd11'],
 ['2', 'hello', 'd', 'even2'],
 ['4', 'hello', 'e', 'even4'],
 ['22', 'hello', 'f', 'even22']]

Upvotes: 1

Samuel LEMAITRE
Samuel LEMAITRE

Reputation: 1041

Try this

x = [['1', 'hello', 'a'], ['3', 'hello', 'b'], ['11', 'hello', 'c'], ['2', 'hello', 'd'], ['4', 'hello', 'e'], ['22', 'hello', 'f']]
y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']
x.sort(key=lambda z: int(z[0]))
for i in range(0, len(x)):
    x[i].append(y[i])
print(x)

output:

[['1', 'hello', 'a', 'odd1'], ['2', 'hello', 'd', 'even2'], ['3', 'hello', 'b', 'odd3'], ['4', 'hello', 'e', 'even4'], ['11', 'hello', 'c', 'odd11'], ['22', 'hello', 'f', 'even22']]

Upvotes: 1

John
John

Reputation: 13699

x = [['1', 'hello', 'a'],
     ['3', 'hello', 'b'],
     ['11', 'hello', 'c'],
     ['2', 'hello', 'd'],
     ['4', 'hello', 'e'],
     ['22', 'hello', 'f']]

y = ['odd1', 'even2', 'odd3', 'even4', 'odd11', 'even22']

x = sorted(x, key=lambda i: int(i[0]))
y = sorted(y, key=lambda i: int(i.replace('even', '').replace('odd', '')))

print y

result = [a + [b] for a, b in zip(x, y)]
print result #prints out [['1', 'hello', 'a', 'odd1'], ['3', 'hello', 'b', 'odd3'], ...]

Upvotes: 1

Related Questions