Python Forum
concat two list based on array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
concat two list based on array
#1
Here is my input
a=[1,2,3,4]
b=[5,6,7,8]
I need the output like this:
Output:
[1,5,2,6,3,7,4,8]
How to do this without build in function
Reply
#2
Please show us an attempt so we can further assist you.
Reply
#3
Hello! Open a Python interpreter, input a=[1,2,3,4] then input dir(a) and see the output. It'll print all the 'a' methods.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Hello Guys!
i just did the following code works as expected.
a=[1,2,3,4]
b=[5,6,7,8]
c=[]
for i in range(len(a)):
c.append(a[i])
c.append(b[i])
print c
Output:
[1, 5, 2, 6, 3, 7, 4, 8]

Thanks for the info #Wavic

how to make this output to look like this:
Output:
[1, 5, 2, 6, 3, 7, 4, 8]
Output:
[(1, 5), (2, 6),(3, 7),(4, 8)]
Reply
#5
There is a better way to do it. Without a loop
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Dec-21-2016, 09:01 AM)MeeranRizvi Wrote:
[(1, 5), (2, 6),(3, 7),(4, 8)]
If this is what you want, look into the zip function.
Reply
#7
Yeah i knew it,
BUt how to do without zip?
Reply
#8
Why would you want to do it without zip?

>>> a = [1, 2, 3, 4]
>>> b = [5, 6, 7, 8]
>>> list(zip(a, b))
[(1, 5), (2, 6), (3, 7), (4, 8)]
>>> [(a[i], b[i]) for i in range(min(len(a), len(b)))]
[(1, 5), (2, 6), (3, 7), (4, 8)]
Reply
#9
Quote:Why would you want to do it without zip?
Because this is a standard way many do education,do task without using stuff(forbid to use) that's build into python.
I do not like this way at all Dodgy 
We even see student that have to use regex with html,because they can not a use parser.
Reply
#10
Can you use enumerate()
Because of I still celebrate here is another way

In [1]: a=[1,2,3,4]

In [2]: b=[5,6,7,8]

In [3]: c = []

In [4]: for index, var in enumerate(a):
   ...:     c.append((var, b[index]))
   ...:    
In [5]: c

Out[5]: [(1, 5), (2, 6), (3, 7), (4, 8)]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  df and regex, NaN and df.concat metalray 8 6,570 Feb-06-2018, 03:29 PM
Last Post: buran
  Using a list like an array pegn305 3 3,127 Nov-23-2017, 06:07 AM
Last Post: cryomick

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020