3

I was trying to read a csv file:

SigGenelist = []
Sig = csv.reader(open('Genelist.csv'))
for row in Sig:
    SigGenelist.append(row)
print (SigGenelist)

the print out was like:

[['x'], ['0610010K14Rik'], ['0610011F06Rik'], ['1110032F04Rik'], ['1110034G24Rik'], ...

so I got a nested list, but I would like to have a list with each element as string, something like:

['x', '0610010K14Rik', '0610011F06Rik', '1110032F04Rik', '1110034G24Rik', ...

I tried to convert element into string like:

SigGenelist = []
Sig = csv.reader(open('Genelist.csv'))
for row in Sig:
    row = str(row) # try to change row into string instead of list
    SigGenelist.append(row)
print (SigGenelist)

but did not get what I would like to...

["['x']", "['0610010K14Rik']", "['0610011F06Rik']", "['1110032F04Rik']","['1110034G24Rik']"...

Any suggestion?

1
  • Be sure to mark an answer as "accepted"! Commented Apr 19, 2017 at 4:08

5 Answers 5

3

Instead of append try using + operator, change your line

...
SigGenelist.append(row)
...

to use +=:

...
SigGenelist += row
...

Append is used to add a single element to your list, while += and extend is used to copy the list on the right hand side to the left hand side. And since extend is more expensive due to an additional function call (not that it matters, difference is very small), += is a good way in your situation.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

my_list = [[1], [2], [3], [4], [5], [6]]
print [item for sublist in my_list for item in sublist]

This is a list comprehension that will flatten a list of lists into a single list.

Or, maybe the simpler option is not appending, but adding the rows to the list.

SigGenelist = []
Sig = csv.reader(open('Genelist.csv'))
for row in Sig:
    SigGenelist += row
print (SigGenelist)

.append will add the entire list to the end of the list, resulting in the nested lists. += will just concatenate the lists, making it a list of depth 1!

2 Comments

Sorry that I did not realize that I could "accept answer" it seemed I could only accept one. But I gave a up vote for your answer. Thanks for the answer and explanation.
No problem! You should always accept the one that answers it the best, and umotto did it very succinctly!
1

Change this:

SigGenelist.append(row)

To this:

SigGenelist.extend(row)

Comments

1

This might be idiomatic:

from itertools import chain
csv_reader = csv.reader(open('Genelist.csv'))
SigGeneList = list(chain.from_iterable(csv_reader))

1 Comment

Not sure I messed up something or not, but it did not work out for me...
0

Use the sum builtin function, since csv.reader returns a generator, the following code will do the job:

Sig = csv.reader(open('Genelist.csv'))
SigGenelist = sum(Sig, []) 

7 Comments

Basically, it worked like other methods, but somehow the first element would not be in the new list...['x', '0610010K14Rik', '0610011F06Rik', '1110032F04Rik', '1110034G24Rik', ...] I only got ['0610010K14Rik', '0610011F06Rik', '1110032F04Rik', '1110034G24Rik', ...], not sure I did not mess up with something... though
the sum(generator, []) is a code snipet I use often. I'm sure there is some code in the middle making some noise.
All solutions seem equal, but you should use the one that is faster and uses less memory. Furthermore, you should also look for readability. Compare the for loops, and initializating a list outside the loop with the "one liners", which one is easier to read? which one has less chances to let you introduce a bug? In this case, I prefer to have the work done in just one line. And from all the other oneliners, I think the option I suggest is the most clear and easy to read, and does not require any other library import. I wouldn't be posting another answer If I thought the others are equal.
You should only execute the two lines in my answer. If you add some extra code in the middle that extract an element of Sig generator, you lose the first item, namely, the 'x'. In fact, I think that directly doing: SigGenelist = sum(Sig, []) should work too.
Yes, they worked.SigGenelist = sum((row for row in Sig), []), SigGenelist = sum(Sig, []), apologize that I messed it up. Thank you for answer and explanation. (no idea why I could not cast a up vote for your answer...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.