I want to reorder a list with length n, where n can be any integer between 5 and 20.
example list = [1,2,3,4,5,6]
for each one of my generated lists, I want to move the last but one element [-2] to the start of the list [0] such that the final order becomes:
new_list = [5,1,2,3,4,6]
I have tried:
myorder =[1,2,3,4,5,6]
new_order = [(myorder[-2]), [i for i in myorder[:-2]], (myorder[-1])]
but this gives me a list in a list:
[5, [1, 2, 3, 4], 6]
Is there a simple way to do this for list comprehension?
You can try this:
new_order = [myorder[-2]] + [i for i in myorder[:-2]] + [myorder[-1]]
Or, simplifying a bit,
new_order = [myorder[-2]] + myorder[:-2] + [myorder[-1]]
You can use list slicing.
Ex:
l = [1,2,3,4,5,6]
print([l[-2]] + l[:-2] + l[-1:])
Output:
[5, 1, 2, 3, 4, 6]
What about this:
>>> lst = [1, 2, 3, 4, 5, 6]
>>> lst = [lst[-2]] + lst[:-2] + [lst[-1]]
>>> lst
[5, 1, 2, 3, 4, 6]
I think this is the most general way of doing it:
example_list.insert(0, example_list.pop(-2))
You erase (pop) item -2 and insert it at position 0.
Related
The case is if I want to reverse select a python list to n like:
n = 3
l = [1,2,3,4,5,6]
s = l[5:n:-1] # s is [6, 5]
OK, it works, but how can I set n's value to select the whole list?
let's see this example, what I expect the first line is [5, 4, 3, 2, 1]
[40]: for i in range(-1, 5):
...: print(l[4:i:-1])
...:
[]
[5, 4, 3, 2]
[5, 4, 3]
[5, 4]
[5]
[]
if the upper bound n set to 0, the result will lost 0. but if n is -1, the result is empty because -1 means "the last one".
The only way I can do is:
if n < 0:
s = l[5::-1]
else:
s = l[5:n:-1]
a bit confusing.
To fully reverse the list by slicing:
l = [1,2,3,4,5,6]
print(l[::-1])
#[6, 5, 4, 3, 2, 1]
If you want to be able to partially or fully reverse the list based on the value of n, you can do it like this:
l = [1,2,3,4,5,6]
def custom_reverse(l,n):
return [l[i] for i in range(len(l)-1,n,-1)]
print(custom_reverse(l,3)) #[6, 5]
print(custom_reverse(l,-1)) #[6, 5, 4, 3, 2, 1]
Hopefully this is what you mean.
print(l[n+1::-1])
I have a list with elements as following:
L =[1, 2, 3, 4, 5]
I want to mirror and reorder as follows:
L =[1,5,2,4,3]
Numbers and size of elements in the list may vary and change!
Having some other examples,
K=[1, 2, 3]
Output may come out as:
K=[1, 3, 2]
And
D=[1,2,3,4]
Final results:
D = [1,4,2,3]
I have tried to do it with slice, it doesn't work for me.
You can do that by merging the list with its reverse:
lst = [1,2,3,4,5]
b = [c for a,b in zip(lst,reversed(lst)) for c in (a,b)][:len(lst)]
print(b) # [1, 5, 2, 4, 3]
Is this what you're looking for?
from random import shuffle
my_list = [1,2,3,4,5]
print (my_list)
shuffle (my_list)
print (my_list)
The following code gives you the expected output.
l = [1,2,3,4,5]
r = []
for i in range(len(l)):
if i % 2 == 0:
r.append(l[i // 2])
else:
r.append(l[-1 - i // 2])
print(r) # prints [1, 5, 2, 4, 3]
What will be the logic or code of this problem? The reverse() function does not take any argument and list[::-1] is also the same. How to do it in easy way?
Example, given:
list = [1,5,4,3,2,6]
Reversing 5...2, the output will be:
list = [1,2,3,4,5,6]
You could use list slice assignment, which modifies the list in-place:
>>> L = [1, 5, 4, 3, 2, 6]
>>> L[1:5] = L[4:0:-1]
>>> L
[1, 2, 3, 4, 5, 6]
You can reassign the slice of the list to that same slice in reverse
l = [1,5,4,3,2,6]
l[1:5] = l[1:5][::-1]
print(l)
#[1, 2, 3, 4, 5, 6]
You could do something like this:
l = [1,5,4,3,2,6]
index5 = l.index(5)
index2 = l.index(2)
l[index5:index2+1] = l[index2:index5-1:-1]
If you want it to be more explicit, you could use reversed:
l[index5:index2+1] = reversed(l[index5:index2+1])
Edit: Sorry, I misunderstood your question.
>>> l = [1, 5, 4, 3, 2, 6]
>>> temp = l[1:5]
>>> temp.reverse()
>>> l[1:5] = temp
>>> l
>>> [1, 2, 3, 4, 5, 6]
I have two lists, one is of a known length and the other is a random length:
MyList1 = [[1, 2, 3], [4, 5],[5, 6, 7, 8]].
MyList2 = [1, 2, 3, 4 ,5 ,6 ]
I need to print the second one in the following way, where 3 lines is for the number of objects in the first list:
[1, 2, 3]
[4, 5]
[6]
The problem is that I don't know the exact length of this list and the sizes of the lines may not be equal.
I got stuck on doing it with for loop, but it doesn't seem to work.
A while loop will do the trick better.
list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5 ,6 ]
start = 0
while start < len(list2):
print list2[start:start+len(list1)]
start += len(list1)
Based on your last comment, I think the following will work for you:
>>> l1
[[1, 2, 3], [4, 5], [5, 6, 7, 8]]
>>> l2
[1, 2, 3, 4, 5, 6]
>>> i,s=0,0
>>> while s < len(l2):
print l2[s:s+len(l1[i])]
s += len(l1[i])
i += 1
[1, 2, 3]
[4, 5]
[6]
If you're curious how to use a for loop:
step_size = int(len(list2)/len(list1))
for i in range(0, len(list1) - 1):
start = i * step_size
end = start + step_size
print(list2[start:end])
print(list2[len(list1)*step_size - step_size:])
The print after the loop prints the last chunk, which might be a different size than the others.
You can try generator function like this,
my_list1 = [1, 2, 3, 4 ,5 ,6 ]
my_list2 = [1, 2, 3]
def make_list(lst1, lst2):
item_count = len(lst1) / len(lst2)
for ix in range(0, len(lst1), item_count):
yield lst1[ix:ix + item_count]
print list(make_list(my_list1, my_list2))
Suppose I have a list x = [1,2,3] and I want to output every other value than the indexed one, is there a list operation I can use?, ie: x[0]=> [2,3], x[1] => [1,3], x[2] =>[1,2] ?
You could use this:
def exclude(l, e):
return [v for i, v in enumerate(l) if i != e]
>>> exclude(range(10), 3)
[0, 1, 2, 4, 5, 6, 7, 8, 9]
You could do it with a slice, but I'd be tempted to try:
a = [1, 2, 3]
b = a[:]
del b[1]
edit
The above does "technically" use a slice operation, which happens to be on list objects in effect a shallow-copy.
What's more flexible and shouldn't have any downsides is to use:
a = [1, 2, 3]
b = list(a)
del b[1]
The list builtin works on any iterable (while the slice operation [:] works on lists or those that are indexable) so that you could even extend it to constructs such as:
>>> a = '123'
>>> b = list(a)
>>> del b[0]
>>> ''.join(b)
'23'
You could use x[:i] + x[i+1:]:
In [8]: x = [1, 2, 3]
In [9]: i = 0
In [10]: x[:i] + x[i+1:]
Out[10]: [2, 3]
Depending on the context, x.pop(i) might also be useful. It modifies the list in place by removing and returning the i-th element. If you don't need the element, del x[i] is also an option.
>>> x = [1, 2, 3]
>>> x[:1] + x[2:]
[1, 3]
my_list = [1, 2, 3]
my_list.remove(my_list[_index_]) #_index_ is the index you want to remove
Such that: my_list.remove(my_list[0]) would yield [2, 3],
my_list.remove(my_list[1]) would yield [0, 3], and
my_list.remove(my_list[2]) would yield [1, 2].
So you want every value except the indexed value:
Where i is the index:
>>> list = [1,2,3,4,5,6,7,8]
>>> [x for x in list if not x == list[i]]
This will give you a list with no instances of the i't element, so e.g.:
>>> i = 2
>>> list = [1,2,3,4,5,6,7,1,2,3,4,5,6,7]
>>> [x for x in list if not x == list[i]]
[1, 2, 4, 5, 6, 7, 1, 2, 4, 5, 6, 7]
note how 3 is not in that list at all.
Every other is
x[::2], start at 0
x[1::2], start at 1