1

Given a list of integers, say, x = [5, 10, 6, 12, 10, 20, 11, 22] write a single expression that returns True if all odd index values are twice their preceding values. We need to use skip slicing, zip, all, and list comprehension

I'm new to Python programming (extensive experience in Java though). This is just a basic question on python syntax, but I'm not able to do it. I tried the following:

list(zip(x[::2], x[1::2]))

this expression returns me a list like below [(5, 10), (6, 12), (10, 20), (11, 22)]

After this I'm lost how to check the condition on the pairs. Looking for something like

print(all([False for pair in list(zip(x[::2],x[1::2]))]) "write something in proper format that checks pair values for double condition")

inferno
  • 55
  • 5
  • "We need to use ..." - this is for learning purposes I suppose? – Jan Stránský Sep 23 '20 at 19:38
  • [https://stackoverflow.com/a/16021600/2823755](https://stackoverflow.com/a/16021600/2823755) ... [https://stackoverflow.com/a/10867891/2823755](https://stackoverflow.com/a/10867891/2823755) – wwii Sep 23 '20 at 19:40
  • Yes, this is for learning purpose. I know this is a very trivial question to ask on this forum, but coming from Java, Python has a lot more of these tricky syntaxes, and I was not able to crack it. Thanks for the help! – inferno Sep 23 '20 at 19:40

3 Answers3

3

Using Zip, Slicing & List Comprehension

x = [5, 10, 6, 12, 10, 20, 11, 22]
all([b == 2*a for a, b in zip(x[::2], x[1::2])])  # True

Explanation

Generator for number pairs

zip(x[::2], x[1::2]) # produces [(5, 10), (6, 12), (10, 20), (11, 22)]

Loop through the tuples (setting to a, b as we go)

for a, b in zip(x[::2], x[1::2]) 

Check condition

b == 2*a   # second number is twice 1st

Check if True everywhere

all(...)
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • Thanks [DarryIG](https://stackoverflow.com/users/3066077/darrylg) for your detailed explanation! – inferno Sep 23 '20 at 19:50
2

This?

x = [5, 10, 6, 12, 10, 20, 11, 22]
result = all(x2 == 2*x1 for x1,x2 in zip(x[::2],x[1::2]))
print(result) # True
Jan Stránský
  • 1,671
  • 1
  • 11
  • 15
  • Your solution is on point. Thanks! However, your solution is iterating over a tuple, say I use this `list(zip(x[::2], x[1::2]))` so this will give me a list like `[(5, 10), (6, 12), (10, 20), (11, 22)]`. How do I do the same thing on list (which has these pairs)? What exactly is this called in Python ? A List of List ?? – inferno Sep 23 '20 at 19:47
  • @inferno `'[(5, 10), (6, 12), (10, 20), (11, 22)]'` is called `str`. `[(5, 10), (6, 12), (10, 20), (11, 22)]` is called `list` (of `tuple`s (of (pair of) `int`s)), depending on how precise you want to be – Jan Stránský Sep 23 '20 at 19:49
1

How about this:

In [123]: x
Out[123]: [5, 10, 6, 12, 10, 20, 11, 22]

In [124]: all([x[i]/x[i-1]==2 for i in range(1, len(x) - 1, 2)])
Out[124]: True
idar
  • 614
  • 6
  • 13
  • I need to use "skip slicing, zip, all, and list comprehension". Your solution does not contain the zip and list methods. Can you provide a solution using these. – inferno Sep 23 '20 at 19:35
  • `all([z/y==2 for y,z in [x for x in zip(x[::2],x[1::2])]])` – idar Sep 23 '20 at 19:46
  • 1
    Yes! this looks more on point. Thanks everyone in the community for helping out :) – inferno Sep 23 '20 at 19:58
  • This is more accurate: `all([y/x==2 for x,y in zip(x[::2],x[1::2])])` – idar Sep 23 '20 at 20:01