name_masked
name_masked

Reputation: 9794

Extract specific columns from list

I have a list that contain the column indexes as follows:

list1 = [0 ,2]

Another list of list would contain the file contents of a csv file as follows:

list2=[["abc", 1, "def"], ["ghi", 2, "wxy"]]

What can be the best way to create a new list that would only contain the values from list2 with the column numbers that are contained in list1 i.e.

newList = [["abc", "def"], ["ghi", "wxy"]]

I am having a hard time creating the sublists

Upvotes: 12

Views: 47907

Answers (6)

Jake P
Jake P

Reputation: 480

You could put Poete Maudit's answer into one line like so:

column = np.array(list_name)[:,column_number].tolist()

You could also keep it as a numpy array by removing .tolist()

Upvotes: 1

Outcast
Outcast

Reputation: 5117

Extracting directly some columns from a python list of lists is not possible because exactly python does not perceive this list as an array (which has by definition rows and columns) but as a list of lists.

However, you can do something like this very easily without using any list comprehension by using Numpy. Specifically, you can do the following:

import numpy as np

list1 = [0 , 2]
list2=[["abc", 1, "def"], ["ghi", 2, "wxy"]]

# Covert list2 to numpy array
array2 = np.array(list2)

# Extract the specific columns from array2 according to list1
newArray = array2[:, list1]

#  Convert the new numpy array to list of lists
newList = newArray.tolist()

# newList is the following list: [['abc', 'def'], ['ghi', 'wxy']]

I hope that this helps too!

Upvotes: 5

Rohit Jain
Rohit Jain

Reputation: 213223

You can use List Comprehension : -

newList = [[each_list[i] for i in list1] for each_list in list2]

Upvotes: 14

mgilson
mgilson

Reputation: 309841

If you are happy with a list of tuples, you can use operator.itemgetter

import operator
list1 = [0,2]
my_items = operator.itemgetter(*list1)
new_list = [ my_items(x) for x in list2 ]

(or you could use map here):

new_list = map(my_items, list2)

and as a 1 liner:

new_list = map(operator.itemgetter(*list1), list2)

operator.itemgetter probably has a slight performance advantage over nested list-comprehensions, but it's likely to be small enough that it's not worth worrying about.

Upvotes: 9

oz123
oz123

Reputation: 28858

If you are working with csv files, you don't need to reinvent the wheel. Take a look at the excellent csv module.

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 113915

>>> list1 = [0 ,2]
>>> list2=[["abc", 1, "def"], ["ghi", 2, "wxy"]]
>>> newList = [[l[i] for i in list1] for l in list2]
>>> print newList
[['abc', 'def'], ['ghi', 'wxy']]

Upvotes: 7

Related Questions