Python Reversing List Using Slicing [duplicate] - python

This question already has answers here:
Python reverse-stride slicing
(8 answers)
Closed 3 years ago.
I am trying to create a function that reverses and mutates a list via slicing/appending without using [::-1] or .reverse(). I am looking for any additional resources online but it seems to be the only two popular reserving techniques.
Can anyone help me think of how I can write this?

I would try:
n = len(a)
for i in range(n):
a.append(a[n - (i+1)])
a = a[n:]

This is one way you can do it:
a = [1, 2, 3, 4]
b = [0]*len(a)
for i in range(len(a)):
b[len(a) - 1 - i] = a[i]
print(b)

Related

Function Return with updated values [duplicate]

This question already has answers here:
What is the difference between `sorted(list)` vs `list.sort()`?
(7 answers)
Closed 4 years ago.
I was trying to sort list elements inside a function without return when I try to print the list by its name it does not get sorted, but it is sorted inside a function.
I have to update the list inside the function without returning
def sort(n):
n.append(10)
sorted(n)
n = [5,1,2,3]
print(n)
Expected : [1,2,3,5]
actual: [5,1,2,3]
I'm sorry, I made a series of mistakes myself. It's a lesson for me too.
def isort(n):
n.append(10)
n.sort() #I used n[:] = sorted(n), but it's superfluous.
n = [5,1,2,3]
isort(n)
print(n)
m = [7,9,3,13]
isort(m)
print(m)
output:
[1, 2, 3, 5, 10]
[3, 7, 9, 10, 13]
sort is an existed fuction in python, need to change it to other name. I changed to isort.
We have to call the function isort to let it do its work.
We need to update the elements in the list to make the list update. [:] Slice notation here.
It's better to use different name between function argument and the call. (n and m here).
Many thanks to DYZ, Primusa, and Tomothy32 :)

Python recursive function to obtain next item on a list [duplicate]

This question already has answers here:
Basics of recursion in Python
(5 answers)
Closed 4 years ago.
I'm quite new to recursion and I have to solve a problem for my homeworks which asks to define a recursive function to get the next element on a given item of a list.
I made the iterative version but I don't understand how to write the recursive one.
def next_value(lst,v):
ind = lst.index(v)
list1_result = lst[ind+1]
return list1_result
a = [4, 2, 10, 3, 2, 5]
print(next_value(a,10))
# output: 3
Your solution seems okay but if it absolutely has to be recursive, here's an example implementation:
def next_value(lst, v):
if (len(lst) < 2):
return None
if (lst[0] == v):
return lst[1]
return next_value(lst[1:], v)
Basically we pass slices from the same list until we find an element with the given value. If the length is less than 2, the list is either empty or we have looked through it all. In that case we return None to denote that there is no valid answer.

Accelerating list concatenation in python 3 [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 4 years ago.
I'm running the following code:
x = []
for i in c:
x = x+i
The result has about 50-100 million elements.
This takes several minutes to run on my PC. How can I accelerate this?
Already compared in join list of lists in python
with Python 2 being faster with .extend than with itertools.chain
An exotique method
l = []
for x in c: l[0:0] = x
among the faster according to
stackoverflow.com/questions/12088089/…
For python 3.5 and latter, even more exotic
l = []
for x in c:
l = [l, *x]
Of course sum(c, []) is among worst on all the measurements.

Assigning two lists as each others equals [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
Closed 4 years ago.
I was just trying to write a code in Python for Euler Problem 15 (https://projecteuler.net/problem=15), concerning Lattice Paths.
I wanted to write something which would return (1,2,1), (1, 3, 3, 1), (1, 4, 6, 4, 1) etc. I wrote the following:
lijst = [1,1]
temp = [1,1]
for a in range(2,21):
temp.append(1)
for b in range(1,a):
temp[b] = lijst[b-1] + lijst[b]
lijst = temp
print a, lijst
To my surprise, this didn't work. At the start of the second 'for'-loop, somehow the append doesn't only work on 'temp', but also on 'lijst'. I got the code working by changing:
lijst = temp
into:
lijst = [a for a in temp]
What's the problem hear? Am I missing something out on the behaviour of lists?
Thanks!

Iterating - Python [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 5 years ago.
Trying to remove negative numbers from a list. I kept running into an issue where two successive negative numbers did not get removed. My code is quite simple:
numbers = [-5, 1, -3, -1]
def remove_neg(num_list):
for item in num_list:
if item < 0:
num_list.remove(item)
print(remove_neg(numbers))
#[1, -1]
I found the answer online after trying 4 different versions of my code and pulling a few hairs out of my head. The answer I found assigned r = numbers[:] and then removed items from r instead of the initial list.
def remove_neg(num_list):
r = numbers [:]
for item in num_list:
if item < 0:
r.remove(item)
print(r)
I understand this concept to have two list variables point to separate data. What I don't understand, is why my initial code doesn't work. Shouldn't for i in numbers: iterate through every item in the list? Why would two successive numbers not be treated the same? I've scoured looking for why and can't seem to find an answer.
In the first example you're modifying the list while iterating over it, which, as you've seen, messes up the iteration. While the second example works, it's very inefficient, and it would be much easier to use a list comprehension to construct a new list without the negatives:
def remove_neg(num_list):
return [x for x in num_list if x > 0]

Categories

Resources