2

Say you have a list of lists, so for example:

my_list = [[1, "foo"], [2, "bar"], [1, "dog"], [2, "cat"], [1, "fox"],
           [1, "jar"], [2, "ape"], [2, "cup"], [2, "gym"], [1, "key"]]

and you wanted to create (in this case two, but could be more) two new distinct lists depending on the first element of each list in my_list, how would you do this?

Of course you could do something like:

new_list1 = []
new_list2 = []
for item in my_list:
    if item[0] == 1:
        new_list1.append(item)
    else:
        new_list2.append(item)

so

new_list1 = [[1, "foo"], [1, "dog"], [1, "fox"], [1, "jar"], [1, "key"]]
new_list2 = [[2, "bar"], [2, "cat"], [2, "ape"], [2, "cup"], [2, "gym"]]

but that is really specific and not very nice in my opinion, so there must be a better way to do this.

user5368737
  • 793
  • 3
  • 12
  • 20

5 Answers5

2
new_list1 = [item for item in my_list if item[0] == 1]

new_list2 = [item for item in my_list if item[0] != 1]

Output :-

[[1, 'foo'], [1, 'dog'], [1, 'fox'], [1, 'jar'], [1, 'key']]

[[2, 'bar'], [2, 'cat'], [2, 'ape'], [2, 'cup'], [2, 'gym']]

Shubham Sharma
  • 1,753
  • 15
  • 24
  • 1
    If you are talking about solution of nbrayns then his solution is wrong. and doesnot give desired output. Try to run it. @Prune – Shubham Sharma Jun 14 '16 at 23:18
  • That, and another solution that has since been deleted. @nbrayns has corrected the posting. – Prune Jun 14 '16 at 23:22
1

This should work:

new_list1 = [i for i in my_list if my_list[0] == 1]
new_list2 = [i for i in my_list if my_list[0] != 1]

There has been some past discussion on this here: Python: split a list based on a condition?

Community
  • 1
  • 1
nbryans
  • 1,507
  • 17
  • 24
1

You can use list comprehension and define a function with 2 parameters as below, first parameter is the original list and second is the key(for example 1, or 2)

    def get_list(original_list, key):
        return [x for x in original_list if x[0] == key]

    print(get_list(my_list, 1))
    print(get_list(my_list, 2))

output:

[[1, 'foo'], [1, 'dog'], [1, 'fox'], [1, 'jar'], [1, 'key']]
[[2, 'bar'], [2, 'cat'], [2, 'ape'], [2, 'cup'], [2, 'gym']]
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
1

A simple solution would be to use a dictionary.

from collections import defaultdict

dict_of_lists = defaultdict(list)
for item in my_list:
    dict_of_lists[item[0]].append(item[1:])

This is nice for the general case where your "ids" can be any object.

If you then want to create variables to store them, you can get the list based on the key you want.

newlist1 = dict_of_lists[1]
newlist2 = dict_of_lists[2]
pushkin
  • 9,575
  • 15
  • 51
  • 95
-2

You might want to make a list of lists, such that the list of entries with key index is in new_list[index].

my_list = [[1, "foo"], [2, "bar"], [1, "dog"], [2, "cat"], [1, "fox"],
           [1, "jar"], [2, "ape"], [2, "cup"], [2, "gym"], [1, "key"]]

new_list = [[x for x in my_list if x[0] == value]
                  for value in range(1 + max([key[0] for key in my_list]))]
new_list1 = new_list[1]
new_list2 = new_list[2]
print new_list1
print new_list2

Output:

[[1, 'foo'], [1, 'dog'], [1, 'fox'], [1, 'jar'], [1, 'key']]
[[2, 'bar'], [2, 'cat'], [2, 'ape'], [2, 'cup'], [2, 'gym']]
Prune
  • 76,765
  • 14
  • 60
  • 81