Home > Back-end >  Selecting 1st element of every list within list
Selecting 1st element of every list within list

Time:10-19

I have the following list of lists with multiple elements:

list = [[1633425661439, 0.11643042583898743],
        [1633428739018, 0.11682454707026001],
        [1633432086311, 0.11950356856187618]]

I want to populate a new_list1 and new_list2 with the first and second numbers within each of those lists, respectively, yielding:

new_list1 = [1633425661439,
            1633428739018,
            1633432086311]

And:

new_list2 = [0.11643042583898743,
            0.11682454707026001,
            0.11950356856187618]

I tried:

for n in list:
        for i in n:
            new_list1.append(i[0])
            new_list2.append(i[1])

But got: TypeError: 'int' object is not subscriptable

CodePudding user response:

You can transpose it like this:

lst = [[1633425661439, 0.11643042583898743],
       [1633428739018, 0.11682454707026001],
       [1633432086311, 0.11950356856187618]]

new_list_1, new_list_2 = map(list, zip(*lst))

And if you are ok with tuples instead of lists, the following will do:

new_list_1, new_list_2 = zip(*lst)

And you really should not name a variable list. It shadows the built-in type.

You can also use simple comprehensions:

new_list_1 = [a for a, _ in lst]
new_list_2 = [a for _, a in lst]

Some docs:

CodePudding user response:

You can try something

list_ = [[1633425661439, 0.11643042583898743],
        [1633428739018, 0.11682454707026001],
        [1633432086311, 0.11950356856187618]]

list_a = [first[0] for first in list_]
list_b = [first[1] for first in list_]

Other way

new_list1 = []
new_list2 = []

for inner_list in list_:
    new_list1.append(inner_list[0])
    new_list2.append(inner_list[1])

CodePudding user response:

You have one level of nesting too much, this would

for n in list:
    for i in n:
        print(i)

would print single elements, which are numbers, you need to do

for n in list:
    new_list1.append(n[0])
    new_list2.append(n[1])

As side note, please avoid using list as it is already used name in python. Overshadowing it might cause unexpected behavior, you can use lst name i.e.:

lst = [[1,2],[3,4],[5,6]]
new_lst1 = []
new_lst2 = []
for n in lst:
    new_lst1.append(n[0])
    new_lst2.append(n[1])
print(new_lst1)
print(new_lst2)

output

[1, 3, 5]
[2, 4, 6]

CodePudding user response:

you can unpack the first and second number in the for loop itself. (BTW best not call the variable "list" because it is the same as a python build-in)

list_ = [[1633425661439, 0.11643042583898743],
        [1633428739018, 0.11682454707026001],
        [1633432086311, 0.11950356856187618]]
new_list1 = []
new_list2 = []
for (i, j) in l:
    new_list1.append(i)
    new_list2.append(j)

CodePudding user response:

  1. Following PEP-8 code style guideline please do not name the variables with reserved keywords like list, dict, for, etc.

  2. With the second loop you iterate over int numbers within the inner lists. If you need to use only the first 2 elements of each list, one loop os enough:

list_ = [
    [1633425661439, 0.11643042583898743],
    [1633428739018, 0.11682454707026001],
    [1633432086311, 0.11950356856187618]]

for inner_list in list_:
    new_list1.append(inner_list[0])
    new_list2.append(inner_list[1])
    

CodePudding user response:

list= [[1633425661439, 0.11643042583898743], [1633428739018, 0.11682454707026001], [1633432086311, 0.11950356856187618]] new_list1 = [ ] new_list2 = [ ] for inner_list in list: new_list1.append(inner_list[0]) new_list2.append(inner_list[1]) print(new_list1) print(new_list2)

  • Related