LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2024, 12:47 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 377

Rep: Reputation: 4
Why append to list is not the same as +=


The first method uses list comprehension, and appends the string in the array 'names' satisfying the given criteria.

First code>
Code:
names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
best_list = [name for name in names if len(name)>=6]
print(best_list)
Output:
Code:
['Kramer', 'Elaine', 'George', 'Newman']
----------------------------------------------------------------------
The second method also does the same, as above.

Second code>
Code:
names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
better_list=[]
for name in names:
    if len(name)>5:
        better_list.append(name)
print(better_list)
Output:
Code:
['Kramer', 'Elaine', 'George', 'Newman']
----------------------------------------------------------------------------------
If change the second code to the one below, then the individual characters, are stored:

Third code>
Code:
names=['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
better_list=[]
for name in names:
    if len(name)>5:
        better_list+=name
print(better_list)
Output:
Code:
['K', 'r', 'a', 'm', 'e', 'r', 'E', 'l', 'a', 'i', 'n', 'e', 'G', 'e', 'o', 'r', 'g', 'e', 'N', 'e', 'w', 'm', 'a', 'n']
-----------------------------------------------------------------------------------------------------
Question: a) Why the third code, saves the individual characters, is unclear.
b) Also, how can the first code be modified to give the same output as the third code?
 
Old 05-14-2024, 02:52 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
yes, that looks strange, but actually a string is an array of chars and += will just concatenate the two arrays (left side + right side).
append will add a single item to the list/array, which is the string itself.
 
Old 05-14-2024, 07:24 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
And this sort of thing is definitely(!) where "D'oh!" problems come about. And why this web-site has both this built-in emoji and this one:

In many languages, not just Python, there are subtly-different things which, "at first glance," appear to be the same. But they're not. So, when you are "reading the source code, looking for that damned bug," you might fail to see it for a long time. Because of course you "see, at a glance" what you expect to see. Even if the language designer thought that some different behavior was "a fee-chur."

In Python, the "+=" operator was so-called overloaded in this way. But it is not the same as .append(). Even though it looks like it is, or ought to be.

And, worse yet – another language-designer might have made an entirely different choice. (In fact, "I once wrote a programming language of my own" , and in that project I did.) So, "your experience with other languages" can actually lead to an unexpected and extremely-annoying surprise.

P.S.: This is one reason why I've introduced all of my programming teams to "the five-minute rule." If you don't see what's wrong with a piece of code after five minutes – and, having tried as best you can to zero-in on where "the bug" must be – ask for another pair of eyes. I explicitly tell the team that "anyone who does this is not 'interrupting you.'"

Last edited by sundialsvcs; 05-14-2024 at 07:32 AM.
 
Old 05-14-2024, 11:31 PM   #4
ajiten
Member
 
Registered: Jun 2023
Posts: 377

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by sundialsvcs View Post
And this sort of thing is definitely(!) where "D'oh!" problems come about. And why this web-site has both this built-in emoji and this one:

In many languages, not just Python, there are subtly-different things which, "at first glance," appear to be the same. But they're not. So, when you are "reading the source code, looking for that damned bug," you might fail to see it for a long time. Because of course you "see, at a glance" what you expect to see. Even if the language designer thought that some different behavior was "a fee-chur."

In Python, the "+=" operator was so-called overloaded in this way. But it is not the same as .append(). Even though it looks like it is, or ought to be.

And, worse yet – another language-designer might have made an entirely different choice. (In fact, "I once wrote a programming language of my own" , and in that project I did.) So, "your experience with other languages" can actually lead to an unexpected and extremely-annoying surprise.

P.S.: This is one reason why I've introduced all of my programming teams to "the five-minute rule." If you don't see what's wrong with a piece of code after five minutes – and, having tried as best you can to zero-in on where "the bug" must be – ask for another pair of eyes. I explicitly tell the team that "anyone who does this is not 'interrupting you.'"
Request if there was a pointer to the source code of overloaded '+' operator, in order to see how the bug appeared.
 
Old 05-15-2024, 01:01 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by ajiten View Post
Request if there was a pointer to the source code of overloaded '+' operator, in order to see how the bug appeared.
it is not a bug, it is how it is implemented, designed. It is intentional. You need to check the python source code yourself if you want to go into details. Do not request again python internals.
 
1 members found this post helpful.
Old 05-15-2024, 04:29 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
@pan64: All I am saying is that “different popular programming language [designers …] might do things very differently. So, a source-code statement that you have written before … in a different world … might now behave differently here. But, “no syntax errors!”

I particularly encountered this issue because I took on “existing [legacy?] software,” in whatever language it might be [have been]. (It isn’t pretty …) Many languages do things like “overloading operators,” but they do it in an interpreter context and the meaning is not always what you(!) “intuitively” expect it to be.

Last edited by sundialsvcs; 05-15-2024 at 04:30 PM.
 
Old 05-15-2024, 05:00 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Originally Posted by ajiten View Post
Request if there was a pointer to the source code of overloaded '+' operator, in order to see how the bug appeared.
You don’t need the source code. You have documentation.

https://python-reference.readthedocs...est/docs/list/
 
Old 05-15-2024, 06:21 PM   #8
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 496

Rep: Reputation: 182Reputation: 182
Yep. Not a bug. Just the way it works. Reference the List document above for working with 'lists'.
 
Old 05-16-2024, 02:25 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by sundialsvcs View Post
@pan64: All I am saying is that “different popular programming language [designers …] might do things very differently. So, a source-code statement that you have written before … in a different world … might now behave differently here. But, “no syntax errors!”

I particularly encountered this issue because I took on “existing [legacy?] software,” in whatever language it might be [have been]. (It isn’t pretty …) Many languages do things like “overloading operators,” but they do it in an interpreter context and the meaning is not always what you(!) “intuitively” expect it to be.
Yes, I know that. It is called: do not assume anything (but read the documentation).
 
Old 05-16-2024, 06:16 AM   #10
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,001

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
Operator overloading is a bad feature, IMO. The available operators are few in number compared to methods. When reading code that uses overloading, one can never be sure what "a+b" does.
Ed
 
1 members found this post helpful.
Old 05-16-2024, 07:15 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,021

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by EdGr View Post
Operator overloading is a bad feature, IMO. The available operators are few in number compared to methods. When reading code that uses overloading, one can never be sure what "a+b" does.
Ed
Not really. As any other thing in this world it can be used properly and can be abused. The other wisdom is: (do not assume anything, but) learn the tool you use otherwise it won't work.
 
Old 05-16-2024, 07:22 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Off: which one is the right usage of the += operator:
Code:
birthday += 3;         /* 3 days, probably */
rocketLaunchTime += 3; /* 3 hours or 3 sec? */

Last edited by NevemTeve; 05-16-2024 at 07:24 AM.
 
Old 05-16-2024, 10:32 AM   #13
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
The only thing I see wrong with the above code sample is that birthday should probably be const.
 
  


Reply

Tags
list, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Type 'sudo' is not known on line 2 in source list /etc/apt/sources.list.d/signal-xenial.list" jameswilson0609 Linux - Newbie 3 05-29-2019 12:04 PM
E: Type 'sudo' is not known on line 1 in source list /etc/apt/sources.list.d/mono-official.list 221B Linux - Newbie 6 09-07-2017 12:14 PM
How to append same data on the same line rhbegin Programming 11 11-24-2011 07:41 PM
Need advice on a script to search many files for list of terms, append hits to list jimmy the saint Programming 1 07-11-2010 03:59 AM
Kate: Append files to existing list and enable the Recent List AGazzaz Linux - Software 2 10-13-2009 08:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration