Reverse a list to a range - python-3.5

I want to reverse a list to a specific range but I am new to Python and I am doing this. Actually i want to reverse a list but to a specific value and I am trying this.
>>> li = [1,2,3,4,5,6]
>>> li = list(reversed(li))
>>> print (li)
[6, 5, 4, 3, 2, 1]
>>> for i in li(range(0,4,1))
SyntaxError: invalid syntax
>>> for i in li(range(0,4,1)):
print(i)

What output are you trying to get?
If you want the certain parts of the list li[2:] will return [4, 3, 2, 1]
and li[:3] will return [6, 5, 4].

You could use slicing and split the list into two for this.
li = [1,2,3,4,5,6]
final_list = li[:li.index(3)]
final_list = final_list.reverse() + li[li.index(3):]

Related

Python} Reversing List Without Using String Method

How should I write the code with the following problem?
Implement the function reverse_print(lst) that prints out the contents of the given list ‘lst’
in reverse order. For example, given a list [3, 6, 2, 1], the output should be 1, 2, 6, 3 (vertical
printout allowed). For this code, you are only allowed to use a single for-loop. Without String Method
Also Not using Print[::-1]
Assuming you are not allowed to just call list.reverse() you can use range with a negative step to iterate the list in reverse order:
def reverse_print(lst):
out = []
for i in range(len(lst) - 1, -1, -1):
out.append(lst[i])
print(*out, sep=", ")
inp = [1, 2, 3, 4]
reverse_print(inp)
Output: 4, 3, 2, 1
You may try something like this
def reverse_print(lst):
rev = [lst[abs(i-l)-1] for i in range(l)]
return rev
lst = [3,6,2,1]
l = len(lst)
print(reverse_print(lst))

How to get combination from 2 list by taking 1 element from list1 and rest of the element from list2 and so on in python

list1 = [1,1,2,4]
list2 = [2,3,5,6]
i would like get all combination like [1,3,5,6], [1,3,5,6] like all combination in python
My question is like list1=[1,2,3,4] list2=[5,6,7,8] I need to see the lists like [1,5,6,7] [5,2,7,8] meaning all possible combination of 2 list.please help
Like this IIUC:
>>> print([[i] + list2[1:] for i in list1])
[[1, 3, 5, 6], [1, 3, 5, 6], [2, 3, 5, 6], [4, 3, 5, 6]]
I think this is what you are looking for:
import itertools
list1 = [1,1,2,4]
list2 = [2,3,5,6]
combination_list = list(itertools.combinations(list1 + list2, 4))
this joins the two lists into one list (i.e, [1,1,2,4,2,3,5,6]) and takes all the 4 element combinations.

How can I sort the list by size?

I am new here and new to Python.
I would like to know if anyone knows how to sort a generated list by size.
I have a piece of the code here. The sorting should be from small to large.
6.52, 26.4
for i in x:
pfad = (i)
title=(i)
size = (i)
bild = (i)
liste=('' + title + '<br>;' + size + '<div><img src="' + bild + '" /></div></br>')
print(liste)
Title<br>;26.14;GB<div><img src="https://URL.jpg" /></div></br>
Title<br>;6.52;GB<div><img src="https://URL.jpg" /></div></br>
<and much more>
I tried with
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste,key=operator.itemgetter(1))
for i in liste:
print (i)
and other things (lambda and so on) but it did not work.
Does anyone have a suggestion?
Greeting Tron
If you are sure of getting just a simple list like [1, 2, 3, 4] then, using sorted() method is in ascending order by default, so no need of a key.
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste)
for i in liste:
print (i)
for descending order add reverse = True into sorting method.
You can retrieve an element from a list with itemgetter() like this:
>>> from operator import itemgetter
>>> z = ['foo', 'bar','qux','zoo']
>>> itemgetter(1)(z)
If still you want to sort with itemgetter you need to have atleast 2 dimensional list
>>> a = [[1,2,1,2,3,4,5,6,3,4],[1,2,3,4],[4,5,7,8], [1,2,2,3,4,5,4,65,3,4,3,2,2]]
>>> sorted(a, key=itemgetter(3))
[[1, 2, 1, 2, 3, 4, 5, 6, 3, 4], [1, 2, 2, 3, 4, 5, 4, 65, 3, 4, 3, 2, 2], [1, 2, 3, 4], [4, 5, 7, 8]]
>>>
But I assume that csv reader will return a dictionary most probably
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste, key=lambda k: k['name of the column'])
for i in liste:
print (i)

Adding the reverse of a list to its end

What's the special function in python that takes a list, or a string, and adds its reverse to the end:
list = [1, 2, 3, 4]
some_function(list):
output: [1, 2, 3, 4, 3, 2, 1]
or string = "1234"
function(string)
output = "1234321"
This one works for both strings and lists :)
def my_reverse(iterable):
for element in iterable[-2::-1]:
iterable += element
return iterable

To make odd position in list go forward for one step

I found this code from stackoverflow ... and wondering how can I move index position that I want.
I tried to use for loop and [::1]. And by making, len(a)*[0]...I couldn't make it.
Is there any way to fix items on its position in list?
Second, without using method below, is there another way to reorder items in list?
'''
mylist=['a','b','c','d','e']
myorder=[3,2,0,1,4]
'''
a = [1,2,3,4,5,6,7]
b = ((a+a[:0:-1])*len(a))[::len(a)][:len(a)]
[1, 7, 2, 6, 3, 5, 4] <=b
[7, 1, 6, 2, 5, 3, 4] <= the result i want
Thanks in advance.
I'm not sure if this is what you want:
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
reversedOrderedList = orderedList[::-1]
finalList = []
for i in range(len(someList)):
if i % 2 == 0:
finalList.append(reversedOrderedList[i//2])
else:
finalList.append(orderedList[i//2])
print(finalList)
output:
[7, 1, 6, 2, 5, 3, 4]
if so, you can write it in shorter way (without reversedOrderedList):
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
finalList = []
for i in range(len(someList)):
if i % 2 == 0:
finalList.append(orderedList[-1-i//2])
else:
finalList.append(orderedList[i//2])
print(finalList)
and from here you can write it without if statement:
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
for i in range(len(someList)):
finalList.append(orderedList[((-1)**(i%2+1)-1)//2 + ((- 1)**(i%2+1))*(i//2)])
print(finalList)
It is not pretty but after that you can easily write a generator.
Zip the list with its reversed version, flatten it and take the first half:
from itertools import chain
a = [1,2,3,4,5,6,7]
b = list(chain.from_iterable(zip(a[::-1], a)))
print(b[:len(b) // 2])
Output
[7, 1, 6, 2, 5, 3, 4]

Resources