Jump to content

Shifting numbers in a list (Python3)

lewdicrous
Go to solution Solved by lewdicrous,
1 hour ago, Meic said:

Frist to print without brackets use:


print(" ".join(lst))

The to shift the other direction (you were almost there)


def shift_right(lst, n):
    return lst[-n:]+lst[:-n]

Rather than passing the newlst to shift, I would recommend:


newlst = shift(lst, n)

 

Solution provided by @Meic

Note, the first code provided has been changed to make it work for my case, from:

print(" ".join(lst))

to:

print(" ".join(map(str, lst)))

 

 

Spoiler

def main():
	lst = [1,2,3,4,5,6,7,8,9]
	print(lst)
	n = int(input("Enter a positive number: "))
	newlst = []
	shift(newlst,lst,n)
	print(newlst)

## shifts the values in the list n times
# @param new list, original list and number of shifts
# @return new list
def shift(newlst,lst,n):
    return newlst.append(lst[n:]+lst[:n])

main()

 

Hello, I'm trying to shift numbers in a list to the right 'n' times, the program I wrote shifts it to the left instead.. How to it to make it work the other way? I also want to print the answer without the brackets

This is what I get:

Spoiler

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter a positive number:  2
[[3, 4, 5, 6, 7, 8, 9, 1, 2]]

 

But I want to get this:

Spoiler

1  2  3  4  5  6  7  8  9
Enter a positive number: 2
8  9  1  2  3  4  5  6  7

 

I tried doing this to make it shift to the right but it didn't work

Spoiler

def shift(newlst,lst,n):
	for i in range(len(lst)-1,0,-n):
		lst[i] = lst[i-n]
		newlst.append(lst[i]) 
		return newlst

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Frist to print without brackets use:

print(" ".join(lst))

 

The to shift the other direction (you were almost there)

def shift_right(lst, n):
    return lst[-n:]+lst[:-n]

 

Rather than passing the newlst to shift, I would recommend:

newlst = shift(lst, n)

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Meic said:

Frist to print without brackets use:


print(" ".join(lst))

 

I get errors when I try this, "TypeError: sequence item 0: expected str instance, int found"

The error goes away if I write "print(" ".join(str(lst)))" but that still prints the results with brackets

The other two solutions work well, thanks :D

 

EDIT:  I solved the problem by writing this:

print(" ".join(map(str, lst)))

Thanks a lot!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Meic said:

Frist to print without brackets use:


print(" ".join(lst))

The to shift the other direction (you were almost there)


def shift_right(lst, n):
    return lst[-n:]+lst[:-n]

Rather than passing the newlst to shift, I would recommend:


newlst = shift(lst, n)

 

Solution provided by @Meic

Note, the first code provided has been changed to make it work for my case, from:

print(" ".join(lst))

to:

print(" ".join(map(str, lst)))

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×