-1
l1 = ['number', '3', 'number', '45', 'animal', 'rat', 'animal', 'tiger', 'color', 'red', 'color', 'orange']

What I want is 3 lists:

num_list = ['3', '45']
animal_list = ['rat', 'tiger']
color_list = ['red','orange']

I've tried:

num_list = [l1[1:4:2]]

this gets the correct elements but not sure how to get the other lists.

magicsword
  • 1,179
  • 3
  • 16
  • 26

5 Answers5

2

Just check at the "word" just before in a loop and append to the correct list.

num_list = list()
animal_list = list()
color_list = list()

i = 0
while i + 1 < len(l1):
    if l1[i] == "number":
        num_list.append(l1[i+1])
    elif l1[i] == "animal":
        animal_list.append(l1[i+1])
    elif l1[i] == "color":
        color_list.append(l1[i+1])
    
    i += 2

If your list will always stay the same (or same "shape") you could use a more precise code (but less efficient and less readable):

num_list = l1[1:4:2]
animal_list = l1[5:8:2]
color_list = l1[9:12:2]

That simply took even item in your list, split into three part. The first one between the item 2 and item 4, the second between items 6 and 8 and then between 10 and 12 (be caution, list start to count at 0, so l1[1] is the second item, this is why your code works).

Adrien Kaczmarek
  • 530
  • 4
  • 13
1

Try this:

l1 = ['number', '3', 'number', '45', 'animal', 'rat', 'animal', 'tiger', 'color', 'red', 'color', 'orange']
res = l1[1::2]
res = [[x, y] for x, y in zip(res[::2], res[1::2])]
print(res)

Output:

[['3', '45'], ['rat', 'tiger'], ['red', 'orange']]
deadshot
  • 8,881
  • 4
  • 20
  • 39
1

You definitely should use dictionaries here. No need of elif What if you have very long list with very many options? This is the way to do:

l1 = ['number', '3', 'number', '45', 'animal', 'rat', 
      'animal', 'tiger', 'color', 'red', 'color', 'orange']

d = {} # create an empty dictionary that will hold all the values for you

for key, value in zip(l1[::2],l1[1::2]):
    if d.get(key) is None:
        d[key] = [] #If the key is missing, then initialize it with an empty list
    d[key].append(value) #append the values 
print(d)
{'number': ['3', '45'], 'animal': ['rat', 'tiger'], 'color': ['red', 'orange']}

if you need exactly like your output, just do:

print(d['number'])
['3', '45']

d['animal']
['rat', 'tiger']

d['color']
['red', 'orange']
Onyambu
  • 67,392
  • 3
  • 24
  • 53
0

A good approach would be to create a dictionary where the key is animal, number, color, etc. and the value is a list.

Simply iterate through the input list. At even iterations save a key and at odd iterations store the value inside a list located by the saved key.

This approach allows you to have as many different categories as you want.

Spidey
  • 894
  • 1
  • 13
  • 29
0

Every even element is a key, every odd is a value. Split lists in keys/values, put them into a dict with list as values and extract the resulting lists from the dict:

l1 = ['number', '3', 'number', '45', 'animal', 'rat', 'animal', 'tiger', 
      'color', 'red', 'color', 'orange']

keys, values = l1[0::2], l1[1::2]

from collections import defaultdict
rv = defaultdict(list)

for k,v in zip(keys,values):
    rv[k].append(v)

num,anim,col = rv.values()

print(num, anim, col, sep="\n")

Output:

['3', '45']
['rat', 'tiger'] 
['red', 'orange']

Readup: How does collections.defaultdict work?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69