Suwan  Wang
Suwan Wang

Reputation: 83

How to extract the first element (as a list of string) of the n-th elements of a nested list, Python

I am a fresh python beginner and trying to extract the first element of the first n-th elements of a nested list. But it doesn't work so far.

Say:

list = [["Anna","w",15],["Peter","m",20],["Zani","m",10], ["Lily","w",19]]

Goal:

list_new = ['Anna','Peter','Zani'...(#until the n-th elements)]

If I want to first element of all the elements in the list, it should be:

for i in list:
    for j in i:
        print j[0]

But what if i only want to strip the first element of the n-th elements of the list, instead of all the elements.

For example for the first 2 elements: I tried:

for i in list[0:2]:
    for j in i:
        print j[0]

but it didn't work.

What's more, if i want to give the value of n later by using

def sheet(list, n) 

and the return statement, how could i do it?

Thank you very much!!

Upvotes: 2

Views: 5120

Answers (4)

Corentin Limier
Corentin Limier

Reputation: 5006

Be careful, list is reserved for the built-in function list() in python, you should not use 'list' as variable name.

To get the first element of each nested list :

l = [["Anna","w",15],["Peter","m",20],["Zani","m",10], ["Lily","w",19]]
for e in l:
     print(e[0])

Prints :

Anna
Peter
Zani
Lily

To do the same on the nth first elements :

def sheet(l, n):
    return [e[0] for e in l[:n]]

sheet(l, 3)

Returns

['Anna', 'Peter', 'Zani']

EDIT

def sheet(l, n):
    for e in l[:n]
        return [e[0]]

This code only returns ['Anna'] because the return statement stops the function. The for loop is stopped at the first element.

def sheet(l, n):
    return [e[0] for e in l[:n]]

is equivalent to :

def sheet(l, n):
    result = [e[0] for e in l[:n]]
    return result

which is equivalent to :

def sheet(l, n):
    result = []
    for e in l[:n]:
        result.append(e[0])
    return result

The for loop can ends before the return statement.

More informations here.

Upvotes: 0

jpp
jpp

Reputation: 164623

You can use a list comprehension. Given an input list of lists L:

L_new = [i[0] for i in L[:n]]

Functionally, you can use operator.itemgetter. In addition, you can use itertools.islice to avoid creating an intermediary list:

from operator import itemgetter
from itertools import islice

L_new = list(map(itemgetter(0), islice(L, 0, n)))

Upvotes: 2

Rarblack
Rarblack

Reputation: 4664

Just try this one which will be easier for you to understand:

n = int(input())

lst = [["Anna","w",15],["Peter","m",20],["Zani","m",10], ["Lily","w",19]]
lst_new = []

for item in lst[:n]:
    name, *rest = item
    lst_new.append(name)

Upvotes: 0

ArtBajji
ArtBajji

Reputation: 960

lst = [["Anna","w",15],["Peter","m",20],["Zani","m",10], ["Lily","w",19]]
n = 2
print(lst[n][0])

Output: Zani

---Updated answer for the details added in the question---

l = [["Anna","w",15],["Peter","m",20],["Zani","m",10], ["Lily","w",19]]
n = 2
for i in range(n):
  print(l[i][0])

Output:

Anna

Peter

Upvotes: 2

Related Questions