Extract a value out of n - python

How do you extract a value out of n of a list in python ?
For example :
n = 3
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
And I would like to get
[0, 3, 6, 9]
I know I can do this with a for, but is there any more pythonic and short way ?

You can do a simple list comprehension
>>> n = 3
>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i for i in l if i%n==0]
[0, 3, 6, 9]
If your list is always like that, then you can use strides
>>> l[::3]
[0, 3, 6, 9]
Tip
Use range to generate lists like that
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Using slicing would be the most pythonic way:
In [1]: n = 3
In [2]: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: l[::n]
Out[3]: [0, 3, 6, 9]

Use a slice with stride:
l[::n]
Demo:
>>> n = 3
>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[::n]
[0, 3, 6, 9]

print l[::3] # slice with step n = 3

Related

Mutually Exclusive Lists Python

I'm trying to simplify this code so it doesn't use two for loops. The aim is to end up with a list of numbers that exist in one list but not the other ie. mutually exclusive.
Here is the code:
list1 = [1, 1, 2, 4, 6, 6, 9]
list2 = [1, 2, 3, 5, 6, 7, 8]
def mutually_exclusive(list1,list2):
new_list = []
for x in list1:
if x not in list2:
new_list.append(x)
else:
pass
for y in list2:
if y not in list1:
new_list.append(y)
else:
pass
return new_list
mutually_exclusive(list1,list2)
and the desired result:
[4, 9, 3, 5, 7, 8]
any help much appreciated thanks.
I have tried zip but doesn't yield all results.
You could also do it like this:
list1 = [1, 1, 2, 4, 6, 6, 9]
list2 = [1, 2, 3, 5, 6, 7, 8]
def mutually_exclusive(list1,list2):
return list(set(list1)^set(list2))
print(mutually_exclusive(list1, list2))
Result:
[3, 4, 5, 7, 8, 9]
You can do the following using symmetric_difference:
l1 = [1, 1, 2, 4, 6, 6, 9]
l2 = [1, 2, 3, 5, 6, 7, 8]
list(set(l1).symmetric_difference(set(l2)))
In [7]: l1
Out[7]: [1, 1, 2, 4, 6, 6, 9]
In [8]: l2
Out[8]: [1, 2, 3, 5, 6, 7, 8]
In [9]: list(set(l1).symmetric_difference(set(l2)))
Out[9]: [3, 4, 5, 7, 8, 9]

Compare two lists and get the indices where the values are different

I would like help with the following situation
I have two lists:
Situation 1:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [0, 1, 2, 3, 5, 5, 6, 7, 8, 9]
I need key output: Key 4 is different
Situation 2:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I need key output: false -> no key is different
Situation 3:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [0, 9, 2, 3, 4, 5, 6, 7, 3, 9]
I need key output: Key 1 and Key 8 is different
How could I resolve this? My array has 260 keys
You can use a list comprehension with zip, and enumerate to get the indices. Use short-circuiting and the fact that an empty list is falsy to get False:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [0, 1, 2, 3, 5, 5, 6, 7, 8, 9]
out = [i for i,(e1,e2) in enumerate(zip(a,b)) if e1!=e2] or False
output:
[4]
output for example #2: False
output for example #3: [1, 8]
An approach with itertools.compress. Compare the lists to check difference in values, pass the result of the comparison to compress which will pick-up only the True ones.
from itertools import compress
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [0, 1, 2, 3, 5, 5, 6, 7, 8, 9]
result = tuple(compress(a, map(int.__ne__, a, b)))
if not result:
print(False)
else:
print(result)
The current answer is really good, but for an approach which doesn't need zip/enumerate, loop like this:
lista = []
listb = []
unequal_keys = []
for idx in range(len(lista)):
if lista[idx] != listb[idx]:
unequal_keys.append(idx)
if unequal_keys == []:
print(False)
else:
print(unequal_keys)
I recommend learning new techniques and using build ins like zip and enumerate though. They are much more pythonic and faster!

slicing a list across several slices

I'm looking to slice a list across two or more slices. For example, there is a list:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Let's say I want to slice the list as items 1 to 4 and 6 to 9.
If we do:
a[1:5]
the output:
[1, 2, 3, 4]
If we do:
a[6:10]
the output is:
[6, 7, 8, 9]
But is there someway to combine multiple slices. Something like:
a[1:5 and 6:10]
to output:
[1, 2, 3, 4, 6, 7, 8, 9]
There is no special syntax, just append the lists slices:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# [1, 2, 3, 4, 6, 7, 8, 9]
print(a[1:5]+a[6:10])
If you want to avoid creating the intermediate lists for the individual slices, you could use itertools.islice and chain.from_iterable to get and combine the slices as iterators.
>>> from itertools import chain, islice
>>> slc = [(1,5), (6,10)]
>>> list(chain.from_iterable(islice(a, *s) for s in slc))
[1, 2, 3, 4, 6, 7, 8, 9]
Also works with 1- or 3-tuples, for just end-, or start-end-step slices.
Based on napuzba's suggestion, I'm thinking that the following might be the most efficient way to do this:
all_slice = [*a[1:5], *a[6:10]]
Where all_slice holds:
[1, 2, 3, 4, 6, 7, 8, 9]
This seems pretty pythonic.
You can use list.extend for this task.
slice1 = a[1:5]
slice2 = a[6:10]
slice1.extend(slice2)
# now use slice1
It appends all the items of the slice2 to the first slice1.
If you have several ranges you are trying to slice, you can use the built-in slice() with a list comprehension:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ranges = [(1, 5), (6, 10)]
[n for s in ranges for n in a[slice(*s)]]
# [1, 2, 3, 4, 6, 7, 8, 9]
Inspired by the answer:
There is no special syntax, just append the lists slices:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(a[1:5]+a[6:10])
FROM -> Aviv Yaniv
b, a = a[1:5], a[6:10]
print(b+a)

Display a List with order modulo N in Python

I want to display a list with order modulo N, for exemple:
With N =6, I have a list l[k]=[1, 2, 3, 4, 5, 6], so I can display its revers l[-k]=[6,5,4,3,2,1] by the instruction l[::-1].
But now I want to display l[(-k)mod N] which is [1,6,5,4,3,2] and then l[(1-k)mod N] which is [2,1,6,5,4,3] and so on.
Is there any instruction in python for display a list like that?
>>> l = [1,2,3,4,5,6]
>>> N = len(l)
>>> revL = l[::-1]
>>> revL
[6, 5, 4, 3, 2, 1]
>>> for i in range(1,N):
... print revL[-i:] + revL[:(N-i)]
...
[1, 6, 5, 4, 3, 2]
[2, 1, 6, 5, 4, 3]
[3, 2, 1, 6, 5, 4]
[4, 3, 2, 1, 6, 5]
[5, 4, 3, 2, 1, 6]

Iterate the start of a list without any import in Python

x = []
for i in l[0::3]:
x.append(i)
for j in l[1::3]:
x.append(j)
for k in l[2::3]:
x.append(k)
print(x)
I want to take a random list like
[1, 2, 3, 4, 5, 6, 7]
that would return
[1, 4, 7, 2, 5, 4, 7]
but for a list of any number. is there a way to increase the start by 1 ?
Using list comprehension:
>>> xs = [1, 2, 3, 4, 5, 6, 7]
>>> [x for i in range(3) for x in xs[i::3]]
[1, 4, 7, 2, 5, 3, 6]

Categories