Python Forum
difference between forms of input a list to function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difference between forms of input a list to function
#3
The be more accurate when do f(b[:] you are making a copy of b then pass if to f.
func-1 you're passing the reference of the list,so the function modifies the original list.
func-2 you're passing a shallow copy of the list to the function,so the original list remains unchanged.

To give an other example,which also is a common mistake(modify a list you loop over) why is albatross in output?
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
for w in words:
    if len(w) > 3:
        words.remove(w)

print(words)
Output:
['albatross', 'dog']
If loop over copy of words then it work.
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
for w in words[:]:
    if len(w) > 3:
        words.remove(w)

print(words)
Output:
['dog']
Or better do not use copy or remove at all.
words = ['eagle', 'albatross', 'mouse', 'dog', 'tiger']
words = [w for w in words if len(w) <= 3]
print(words)
Output:
['dog']
Reply


Messages In This Thread
RE: difference between forms of input a list to function - by snippsat - Oct-04-2023, 01:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  user input values into list of lists tauros73 3 1,126 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,164 Dec-25-2022, 03:00 PM
Last Post: askfriends
  set.difference of two list gives empty result wardancer84 4 1,575 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Showing an empty chart, then input data via function kgall89 0 1,005 Jun-02-2022, 01:53 AM
Last Post: kgall89
  input function question barryjo 12 2,879 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,267 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,228 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 3,007 Oct-10-2021, 12:55 AM
Last Post: Turtle
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 2,014 Oct-06-2021, 09:39 AM
Last Post: menator01
  Input function cutting off commands at spaces. throwaway34 3 2,262 May-12-2021, 06:40 AM
Last Post: throwaway34

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020