-2

I want to store even number and odd number in a separate list. But, here I am facing a unique problem. I am able to store it in sets but not in lists. Is there a way wherein I can store these in a List without repetition.

I have tried this in Jupyter notebook

list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
for i in list_loop:
    if i % 2 == 0 :
        list_even = list_even + [i]
    else:
        list_odd = list_odd + [i]
print(set(list_even))
print(set(list_odd))

Expected output:

[2,4,6,8,10,12]
[1,3,5,7,9,11,13,17,51]
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

5 Answers5

0

You can apply the list function to your set object in order to convert it to a list.

list_from_set = list(set(list_even))
>>> print(list_from_set)
[2, 4, 6, 8, 10, 12]
Lante Dellarovere
  • 1,838
  • 2
  • 7
  • 10
0

There are a couple of ways you could do this. You could use the OrderedDict in the collections library, or you could just sort the set and get a list,

...
print(sorted(set(list_even)))
print(sorted(set(list_odd)))

Also, I would personally create those lists using a set comprehension

list_even = sorted({x for x in list_loop if x % 2 == 0})
list_odd = sorted({x for x in list_loop if x % 2 == 1})
jdowner
  • 718
  • 1
  • 8
  • 21
0

Use a comprehension

>>> list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
>>> print(list(set(_ for _ in list_loop if _ % 2)))
[1, 3, 5, 7, 9, 11, 13, 17, 51]

Similarly for even numbers.

Jens
  • 8,423
  • 9
  • 58
  • 78
0

Define list_odd and list_even as lists and don't convert them to sets before printing. Note that you can use list comprehension to fill list_odd and list_even:

list_odd = []
list_even = []

list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
list_odd = [elem for elem in list_loop if elem % 2 != 0]
list_even = [elem for elem in list_loop if elem % 2 == 0]

print(list_even)
print(list_odd)

Output:

[2, 4, 6, 8, 10, 12, 4, 6]
[1, 3, 5, 7, 9, 11, 13, 1, 1, 51, 17]

Edit: for uniqueness, turn list_loop into a set:

list_loop=set([1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,])

Output:

[2, 4, 6, 8, 10, 12]
[1, 3, 5, 7, 9, 11, 13, 17, 51]
glhr
  • 4,439
  • 1
  • 15
  • 26
0

You can solve this using a list comprehension with a filter condition - but you then iterate your list twice.

By using a simple for loop you only need to touch any number once at it will conserve the original order - what putting your numbers through a set might not do - order in a set is not guaranteed:

Keep a set of seen numbers, only add anything if your current number was not yet seen.

list_loop = [1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]

list_even = []
list_odd = [] 
seen = set()

trick = [list_even, list_odd]  # even list is at index 0, odd list at index 1

for i in list_loop:
    if i in seen: 
        continue
    else:
        seen.add(i)
                                 # the trick eliminates the need for an if-clause
        trick[i%2].append(i)     # you use i%2 to get either the even or odd index


print(list_even)
print(list_odd)

Output:

[2, 4, 6, 8, 10, 12]
[1, 3, 5, 7, 9, 11, 13, 51, 17]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69