Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:26:13+00:00 2026-06-15T03:26:13+00:00

Coming from other languages, I am a little bit confused about the way Python

  • 0

Coming from other languages, I am a little bit confused about the way Python variable assignments in lists. As an example, let’s say:

x = [4, 5, 2, 70, 1]
y = x
y.sort()

If x and y are printed, the result is the same for both variables:

x = [1, 2, 4, 5, 70]
y = [1, 2, 4, 5, 70]

I did not quite expect this behavior. I thought the sequence of x would not be changed, since I applied the sort method only on list y.

On the other hand, if I had assigned the contents of list x to list y using slicing operators, then I would achieve the expected (at least in my case) behavior:

x = [4, 5, 2, 70, 1]
y = x[:]
y.sort()

If x and y are printed, I see that list x remains untouched.

x = [4, 5, 2, 70, 1]
y = [1, 2, 4, 5, 70]

Can somebody explain the logic behind?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:26:14+00:00Added an answer on June 15, 2026 at 3:26 am

    Slicing makes a copy whereas assignment points both labels to the same list instance. Sorting y sorts the list instance, which is the same instance pointed to by x. Alternatively, you could use y = sorted(x) to get the result you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Python for about a year now, coming from a mostly
I'm working on improving my skills in other languages, coming from using c++ as
In R programing for those coming from other languages John Cook says that R
Coming from various other languages, I find R powerful and intuitive, but I am
I want to compare two arrays, one coming from a shoppingcart and the other
Okay: I'm fairly new to C++ and static languages on a whole. Coming from
I learning Python (coming from a dotnet background) and developing an app which interacts
I am trying to implement a Queue in C. Coming from Java and other
Coming from Java or Python I am used to making such constructs and normally
I am thinking about learning Clojure, but coming from the c-syntax based (java, php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.