-5

I´m new to python and don´t know what to search for in forums i just know the specific problem i have, so when i searching around i don´t find what im lokking for... so please how do i join two arrays into one list or tuple?

file = [] # a,b,c,d,e,f,..
date = [] # 1,2,3,4,5,6,...
array_list = [a,1],[b,2],[c,3],....

Is it even possible to do that?

fili
  • 39
  • 1
  • 9

1 Answers1

0

Simply you could merge two lists using the built-in zip function in python,

list(zip(file, date))
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • It will give output like [(1, 'a'), (2, 'b'), (3, 'c')] not like [[1, 'a'], [2, 'b'], [3, 'c']] . – Aakash Goel Jun 28 '17 at 12:03
  • `import numpy as np a=np.array([1,2,3]) b=np.array(['a','b','c']) c=zip(a,b) print c ` And You would get output as below: ` [(1, 'a'), (2, 'b'), (3, 'c')] ` However, you can convert your, tuples in to list too like as below: ` new_output = map(lambda x:list(x),c) print new_output ` And You would get output as below: [[1, 'a'], [2, 'b'], [3, 'c']] – Aakash Goel Jun 28 '17 at 12:03
  • 1
    You've made your point, but writing so much code for just converting an immutable item into a mutable one, can be termed unnecessary, where tuples can perform what lists are meant to be. – zaidfazil Jun 28 '17 at 12:08
  • Actual code is very small, It seeming you long because i have declared variable, library import (Numpy) and all. However, single line code is `map(lambda x:list(x),zip(file,date)) ` – Aakash Goel Jun 28 '17 at 12:13
  • 2
    @AakashGoel or rather `map(list,zip(file,date))` but to get a real list in python 3 you have to do `[list(x) for x in zip(file,date)]` – Jean-François Fabre Jun 28 '17 at 12:14
  • @Jean-FrançoisFabre Above code `map(lambda x:list(x),zip(file,date))` is according to python 2.7 Version. – Aakash Goel Jun 28 '17 at 12:17
  • 1
    my point: `lambda x:list(x)` => just `list` – Jean-François Fabre Jun 28 '17 at 12:20