A
A
Artemy2022-01-13 20:19:15
Python
Artemy, 2022-01-13 20:19:15

How to output a list with one tuple of three, in which the list consists of even numbers?

There is a list with tuples, blackjack and... numbers

l = [("a", [1, 3, 5, 6, 7]), ("b", [1, 2, 3, 4, 5]), ("c", [2, 4, 6, 8])]

What is the most correct way to write it so that it outputs a list with the tuple in which the list consists of even numbers? In easy, you can write such a terrible loop:
[("c", [2, 4, 6, 8])]

for i in range(len(l)):
    for k in range(len(l[i][1])):
        if l[i][1][k] % 2 != 0:
            break
        else:
            if k == len(l[i][1]) - 1:
                l = [l[i]]
                print(l)

This, of course, is no good, only for problems on CodeWars xD
How can you correctly, beautifully, quickly use nested lists, lambdas?
Help..
I've been scratching my head for 5 hours now :\

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MinTnt, 2022-01-13
@Fepler

If it means that all elements are even, then mb is something like ..

l = [("a", [1, 3, 5, 6, 7]), ("b", [1, 2, 3, 4, 5]), ("c", [2, 4, 6, 8])]

def func(l):
  for x in l:
    if all(not i%2 for i in x[1]):
      return x

print(func(l))

S
Sergey Karbivnichy, 2022-01-13
@hottabxp

for x in l:
  if len(x[1]) %2 ==0:
    print(x)
or:
s = [x for x in l if len(x[1]) %2 == 0 ]
print(s)

A
aromensky, 2022-01-13
@aromensky

I always write bad code, but this is what I would do.

l = [("a", [1, 3, 5, 6, 7]), ("b", [1, 2, 3, 4, 5]), ("c", [2, 4, 6, 8]), ("g", [2, 3, 6, 8])]
A = []

for SAS in l:
    Q = True
    for SUS in SAS[1]:
        if SUS % 2 != 0:
            Q = False
            break
    if Q == True:
        A.append(SAS[0])

print(A)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question