say you have a list:

``list = [1,a,2,b,3,c,4,d,5,e,6,f,7,g,8,h,9,i,10,j,11,k,12,l,13]``

What's the easiest loop set up if I want to store each number (every other slot) into another list? The above list is actually text from a file read and then split with "1 a" and such on each line of the file. After I do that, I want the other slots put into another list. Also, let's say the above letters are different and there are multiples of the same letter, I want to delete the multiples and be left with a list of all the unique letters in the original list.

Ex: if the list is [1,a,2,a,3,v,4,t,5,v,9,c], I want [1,2,3,4,5,9] and [a,c,t,v] (numerical and alphabetic order respectively)

Thanks

say you have a list:

``list = [1,a,2,b,3,c,4,d,5,e,6,f,7,g,8,h,9,i,10,j,11,k,12,l,13]``

What's the easiest loop set up if I want to store each number (every other slot) into another list? The above list is actually text from a file read and then split with "1 a" and such on each line of the file. After I do that, I want the other slots put into another list. Also, let's say the above letters are different and there are multiples of the same letter, I want to delete the multiples and be left with a list of all the unique letters in the original list.

Ex: if the list is [1,a,2,a,3,v,4,t,5,v,9,c], I want [1,2,3,4,5,9] and [a,c,t,v] (numerical and alphabetic order respectively)

Thanks

hmm.list comprehension?

``````alpha = [ c for c in list if str(c).isalpha()]
number = [ c for c in list if str(c).isdigit()]``````

if you want to get unique elements, use set

This tiny code snippet will separate your list and make the alpha list unique. I also added the typical list sort:

``````raw_list = [1, 'a', 2, 'a', 3, 'v', 4, 't', 5, 'v', 9, 'c']

alpha_list = []
number_list = []
for c in raw_list:
# make the alpha_list unique
if alpha_list.count(c) < 1 and str(c).isalpha():
alpha_list.append(c)
elif str(c).isdigit():
number_list.append(c)

# sort list in place
alpha_list.sort()

print alpha_list   # ['a', 'c', 't', 'v']
print number_list  # [1, 2, 3, 4, 5, 9]``````

Added php tags to make Python code color highlighted.

Thanks. That was helpful, but I don't understand this line:

``if alpha_list.count(c) < 1``
``alpha_list.count(c)``

... simply counts the number of times c is in the list. If the count < 1 then it isn't present yet and you can append the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.