1

I have created the for loop to "extract" column from list of lists, now I want to assign this list to a variable. How to do it?

I have the following for loop:

j = 1
for i in range(len(table)):
    row = table[i]
    print(row[j])

and the table looks like:

NAME
Bart First
Maria Great
Theresa Green

I would like to do some other "operations" on that column and I guess would be easy to use functions if that column is assign to variable...but I have no idea how to do it. (I must not use numpy or pandas for this).

1
  • @martineau whoops, we edited at the same time. It's rolled back to your version. Commented Apr 2, 2021 at 10:11

2 Answers 2

1

Solution with minmal change of original: create list before loop and append to it i.e.:

j = 1
list1 = []
for i in range(len(table)):
    row = table[i]
    list1.append(row[j])
print(list1)

Note that you might use for to access element directly rather than using index, i.e. loop might be replace with

for row in table:
    list1.append(row[j])
Sign up to request clarification or add additional context in comments.

Comments

0

Wrt "I guess would be easy to use functions if that column is assign to variable": You could use a namedtuple which is a Python built-in to give these things "names". Essentially, you would be converting each row into a tuple (instead of a list) and each part of that tuple would be accessible by name as well as index instead of just index.

For that you'd first need to assign each row of table to a namedtuple. Without more details in your post about what table contains, apart from NAME, I'll make assumptions about your data:

import collections

# example `table` data
table = [[1, 'Bart First', 30, 'UK'],
         [2, 'Maria Great', 25, 'US'],
         [3, 'Theresa Green', 20, 'PL']]

# the namedtuple "structure" which will hold each record:
Person = collections.namedtuple('Person', 'id name age country')

people = []  # list of records
for row in table:
    person = Person(*row)  # convert it to a namedtuple
    people.append(person)

# or the four lines above in one line:
people = [Person(*row) for row in table]

# or assign it back to `table`:
# table = [Person(*row) for row in table]

people would now look like:

[Person(id=1, name='Bart First', age=30, country='UK'),
 Person(id=2, name='Maria Great', age=25, country='US'),
 Person(id=3, name='Theresa Green', age=20, country='PL')]

Next, to get just the names:

all_names = []
for person in people:
    all_names.append(person.name)

print(all_names)
# output:
# ['Bart First', 'Maria Great', 'Theresa Green']

# or as a list comprehension:
all_names = [person.name for person in people]

Since you mentioned you can't use pandas or numpy, that would prevent you from doing certain things like sum(people.age) or people.age.sum() but you could instead do

>>> sum(person.age for person in people)
75

And if you still need to use the index, then you can get the country data (4th column, index=3):

>>> col = 3
>>> for person in people:
...     print(person[col])
...
UK
US
PL

Comments

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.