1
LISTA=[["001", "TOM", "13800000001", "AAAA"],["002", "Jerry", "13800000002", "BBBB"]]
name=[]
for ID,NAME,HSNUMBER,ADDRESS in LISTA:
    name.append(NAME)

>>> name
['TOM', 'Jerry']

I feel it is not a simple way to get all the NAME in LISTA,how to revise it ?

showkey
  • 482
  • 42
  • 140
  • 295

2 Answers2

1

You know that the name will be the second element in the lists, so you can use list comprehension, like this

names = [item[1] for item in LISTA]
print names
# ['TOM', 'Jerry']
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

I think this is very simple way.

>>> LISTA=[["001", "TOM", "13800000001", "AAAA"],["002", "Jerry", "13800000002", "BBBB"]]
>>> name = zip(*LISTA)[1]
('TOM', 'Jerry')

UPDATE

what is the meaning of * here?

In general

x = func(*[a, b, c])

is equivalent to

x = func(a, b, c)

So here

#zip(*LISTA)
zip(*[["001", "TOM", "13800000001", "AAAA"],["002", "Jerry", "13800000002", "BBBB"]])

is equivalent to

zip(["001", "TOM", "13800000001", "AAAA"],["002", "Jerry", "13800000002", "BBBB"])
Kei Minagawa
  • 4,395
  • 3
  • 25
  • 43