Nov. 17, 2023, 11:54 a.m. | suvhotta

DEV Community dev.to




My Initial Struggle with List Copying


When I began my programming journey in Python, I encountered a scenario that many new programmers face: copying a list and passing it to a function for further modification. Here's the approach I initially used and why it didn't work as expected.




def square_nums(nums_list):
...

original_list = [1, 2, 3, 4, 5]

copied_list = original_list

squared_nums = square_nums(copied_list)



However, I soon realized a critical issue: any modifications made to copied_list within the function were …

began beginners face function journey list programmers programming python struggle understanding webdev work

Machine Learning Postdoctoral Fellow

@ Lawrence Berkeley National Lab | Berkeley, Ca

Senior Data Engineer (Microsoft Azure)

@ Capco | UK - London

Senior Data Analyst

@ Publicis Groupe | Bengaluru, India

Senior Data Engineer

@ Press Ganey | Chicago, IL, United States

Senior Data Scientist (remote from EU)

@ PriceHubble | Vienna, Vienna, Austria - Remote

Data Science Co-op

@ Novelis | Atlanta, GA, United States