2

I have multiple 1-element lists that I wish to convert into 1 single list:

Current lists:

['orange']
['apple']
['grape']
['banana']
['mango']

Wish to have an output like this:

['orannge', 'apple', 'grape', 'banana', 'mango']

My purpose is to write those array with 1x3 into a csv file

Here is Code:

import csv
#import numpy as np

f1 = open('out.csv')
data = csv.reader(f1, delimiter=':')
print (data)
for row in data:
    #print(row)
    modules = print(row[2:])
    #for row in module:

    result = [[0,0]]

# iterate through rows
for i in modules:
    # iterate through columns
    for j in modules[0]:
        result[j][i] = X[i][j]

for r in result:
    print(r)  

#Try with zip/map
#print(map(data,zip(*module))
#with open('test.csv', 'wb') as f:
    #writer = csv.writer(f)
    #for row in module:
    #writer.writerow(module)
f1.close()
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
iop
  • 39
  • 1
  • 7
  • Could you tell us a bit more about what you already tried? – dazedconfused Jun 21 '16 at 05:41
  • I tried with transpose matrix(error), numpy (i am using python 3 fail to install numpy), map/zip etc – iop Jun 21 '16 at 05:45
  • From the context of your question, it's not clear why you would use matrix or numpy to achieve this task; you might want to elaborate your question and maybe provide some codes you've written – dazedconfused Jun 21 '16 at 05:49
  • How do you keep those arrays currently? Are they in a list of lists like [["..."],["...],] or are they named entities like a = ["orange"] ? How many lists are we talking about aprox. ? – Tino A. Jun 21 '16 at 06:02
  • I am keeping them in [["..."],["...],] approximately got 1k data. I would like to write those 1x10000 array form data in csv file – iop Jun 21 '16 at 06:06
  • Your code seems to be doing something not related to your description. I think you can get rid of it. – Sнаđошƒаӽ Jun 21 '16 at 06:15
  • Do you have any idea how too do this? I read a csv file with 1000X1 then i wish to convert it into 1x1000 then write into csv file again – iop Jun 21 '16 at 06:31

4 Answers4

0

Since you are keeping a list of lists just flatten the list like so:

inputlist = [['orange'],['apple'],['grape'],['banana'],['mango']]
outputlist = []
for i in inputlist:
  outputlist.append(i[0])
Tino A.
  • 232
  • 3
  • 12
  • above code works, but with my input file it got an error "TypeError: 'NoneType' object is not iterable" – iop Jun 21 '16 at 06:29
  • Can you edit your question with the code you used? Are you extracting the lists from a file or where do you get them from? – Tino A. Jun 21 '16 at 06:31
  • yes, i am extracting from a csv file with a data of 1000x1 but i want to convert it into 1x1000 then write into a file – iop Jun 21 '16 at 06:33
  • `modules = print(row[2:])` I think this line is your mistake. Why use print there? Try it without it. – Tino A. Jun 21 '16 at 06:43
  • modules is a list of array that store my data, i have listed as above, my modules input data is same as your inputlist – iop Jun 21 '16 at 07:01
0

Based on your original question, if you stored them in array lst then the below would do what you want

NewArray =[lst[i][0] for i in range(0,len(lst)) ]
user3404344
  • 1,707
  • 15
  • 13
0

To convert your 1-element lists into a single list you can do this:

mylists = [
    ['orange'],
    ['apple'],
    ['grape'],
    ['banana'],
    ['mango']
]

l = []
for item in mylists:
    l.append(item[0])

print(l)

Output:

['orange', 'apple', 'grape', 'banana', 'mango']
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • TypeError: 'NoneType' object is not iterable – iop Jun 21 '16 at 06:26
  • for which line do you get this error? BTW, have you made sure that your `mylists` is not `None`? – Sнаđошƒаӽ Jun 21 '16 at 06:30
  • I get an error in for item in mylists:, i replace mylists with modules – iop Jun 21 '16 at 06:34
  • How are you getting the values of `modules`? From a function? If so make sure that the function is not returning `None` – Sнаđошƒаӽ Jun 21 '16 at 06:37
  • modules is a list of array that store my data, i have listed as a bove – iop Jun 21 '16 at 06:42
  • You need to edit your question, and add the code relevant to your problem. Also show the exact structure of the input data (in your case `modules`). – Sнаđошƒаӽ Jun 21 '16 at 06:46
  • my modules data is like your mylists which is same as my question – iop Jun 21 '16 at 06:56
  • No, they are not same, there is a slight difference. Hence I am guessing there are more important things to consider. So please edit your question and add necessary details. BTW, try and run the code I have given. It should run without problem. So problem must be in some other part of your code. – Sнаđошƒаӽ Jun 21 '16 at 07:00
  • my modules is same as my question above which is a non type not a sequence. I have to Convert NoneType value to sequence string then only can perform with your code – iop Jun 21 '16 at 10:02
0

@Tino is asking a valid question. To make it robust, you need to provide from that source the list is coming from. but for now, you can simple append each item individually into a list like;

one = []
one.append(a.__getitem__(0) )
one.append(b.__getitem__(0) )
one.append(c.__getitem__(0) )
one.append(d.__getitem__(0) )
one.append(e.__getitem__(0) ) 
print one 

if you have the item elements list in a sub list, then you can use for loop;

my_list = [['orange'],['apple'],['grape'],['banana'],['mango']]
for n in my_list:
    one.append(n[0]) 
Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54