Re: list of the lists - append after search
On Thursday, March 2, 2017 at 9:33:14 PM UTC+5:30, Andrew Zyman wrote: > Hello, > please advise. > > I'd like search and append the internal list in the list-of-the-lists. > > Example: > ll =[ [a,1], [b,2], [c,3], [blah, 1000] ] > > i want to search for the internal [] based on the string field and, if > matches, append that list with a value. > > if internal_list[0] == 'blah': > ll[ internal_list].append = [ 'new value'] > > End result: > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] > > > I came up with the following, but the second stmnt is not correct: > > print [x for x in ll if x[0]== 'blah'] > print ll.index([x for x in ll if x[0]=='blah']) > > output: > ValueError: [['blah', 1]] is not in list > > > > > thank you > AZ list_of_lists = [['a', 1], ['b', 2], ['c', 3], ['blah', 1000]] for sublist in list_of_lists: if sublist[0] == 'blah': sublist.append('new value') Hope that helps. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
On Thursday, March 2, 2017 at 3:53:25 PM UTC-5, Andrew Zyman wrote: > On Thursday, March 2, 2017 at 3:31:36 PM UTC-5, Jussi Piitulainen wrote: > > Andrew Zyman writes: > > > > > On Thursday, March 2, 2017 at 2:57:02 PM UTC-5, Jussi Piitulainen wrote: > > >> Peter Otten <__pete...@web.de> writes: > > >> > > >> > Andrew Zyman wrote: > > >> > > > >> >> On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: > > >> >>> Andrew Zyman wrote: > > >> >>> . > > >> >>> . > > >> >>> > End result: > > >> >>> > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] > > >> >>> > > >> >>> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > > >> >>> >>> for inner in outer: > > >> >>> ... if inner[0] == "blah": > > >> >>> ... inner.append("new value") > > >> >> > > >> >> thank you. this will do. > > >> >> Just curious, is the above loop can be done in a one-liner? > > >> > > > >> > Ah, that newbie obsession ;) > > >> > > > >> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > > >> [inner + ["new value"] if inner[0] == "blah" else inner for inner > > >> in > > >> > outer] > > >> > [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > > >> > > > >> > Note that there is a technical difference to be aware of -- matching > > >> > lists are replaced rather than modified. > > >> > > >> I take it you are too sane, or too kind, to suggest the obvious > > >> solution: > > >> > > >> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > > >> >>> [inner.append("new value") for inner in outer if inner[0] == "blah"] > > >> [None] > > >> >>> outer > > >> [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > > >> > > >> [snip] > > > > > > Arh!!! this is it :) > > > > > > I'm sure i'll regret this line of code in 2 weeks - after i > > > successfully forget what i wanted to achieve :) > > > > Jokes aside, you should strive to express your intention in your code. > > Be kind to your future self. Write the three-line loop if you want > > in-place modification. > > > > I use comprehensions a lot, but not to save lines. I might make Peter's > > expression above a four-liner to make its structure more visible: > > > > res = [ ( inner + ["new value"] > > if inner[0] == "blah" > > else inner ) > > for inner in outer ] > > > > Maybe. > > thank you gentlemen. This is very helpful discussion and i appreciate your > explanations - helped a lot. Well, i'll take my earlier statement ( about using lists ) back . 100K search/expend on list is taking way too much time ( over 4 minutes ). i'll follow your advice and move the processing to dict. Thanx again. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
On Thursday, March 2, 2017 at 3:31:36 PM UTC-5, Jussi Piitulainen wrote: > Andrew Zyman writes: > > > On Thursday, March 2, 2017 at 2:57:02 PM UTC-5, Jussi Piitulainen wrote: > >> Peter Otten <__pete...@web.de> writes: > >> > >> > Andrew Zyman wrote: > >> > > >> >> On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: > >> >>> Andrew Zyman wrote: > >> >>> . > >> >>> . > >> >>> > End result: > >> >>> > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] > >> >>> > >> >>> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > >> >>> >>> for inner in outer: > >> >>> ... if inner[0] == "blah": > >> >>> ... inner.append("new value") > >> >> > >> >> thank you. this will do. > >> >> Just curious, is the above loop can be done in a one-liner? > >> > > >> > Ah, that newbie obsession ;) > >> > > >> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > >> [inner + ["new value"] if inner[0] == "blah" else inner for inner in > >> > outer] > >> > [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > >> > > >> > Note that there is a technical difference to be aware of -- matching > >> > lists are replaced rather than modified. > >> > >> I take it you are too sane, or too kind, to suggest the obvious > >> solution: > >> > >> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > >> >>> [inner.append("new value") for inner in outer if inner[0] == "blah"] > >> [None] > >> >>> outer > >> [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > >> > >> [snip] > > > > Arh!!! this is it :) > > > > I'm sure i'll regret this line of code in 2 weeks - after i > > successfully forget what i wanted to achieve :) > > Jokes aside, you should strive to express your intention in your code. > Be kind to your future self. Write the three-line loop if you want > in-place modification. > > I use comprehensions a lot, but not to save lines. I might make Peter's > expression above a four-liner to make its structure more visible: > > res = [ ( inner + ["new value"] > if inner[0] == "blah" > else inner ) > for inner in outer ] > > Maybe. thank you gentlemen. This is very helpful discussion and i appreciate your explanations - helped a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
Andrew Zyman writes: > On Thursday, March 2, 2017 at 2:57:02 PM UTC-5, Jussi Piitulainen wrote: >> Peter Otten <__pete...@web.de> writes: >> >> > Andrew Zyman wrote: >> > >> >> On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: >> >>> Andrew Zyman wrote: >> >>> . >> >>> . >> >>> > End result: >> >>> > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] >> >>> >> >>> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >> >>> >>> for inner in outer: >> >>> ... if inner[0] == "blah": >> >>> ... inner.append("new value") >> >> >> >> thank you. this will do. >> >> Just curious, is the above loop can be done in a one-liner? >> > >> > Ah, that newbie obsession ;) >> > >> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >> [inner + ["new value"] if inner[0] == "blah" else inner for inner in >> > outer] >> > [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] >> > >> > Note that there is a technical difference to be aware of -- matching >> > lists are replaced rather than modified. >> >> I take it you are too sane, or too kind, to suggest the obvious >> solution: >> >> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >> >>> [inner.append("new value") for inner in outer if inner[0] == "blah"] >> [None] >> >>> outer >> [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] >> >> [snip] > > Arh!!! this is it :) > > I'm sure i'll regret this line of code in 2 weeks - after i > successfully forget what i wanted to achieve :) Jokes aside, you should strive to express your intention in your code. Be kind to your future self. Write the three-line loop if you want in-place modification. I use comprehensions a lot, but not to save lines. I might make Peter's expression above a four-liner to make its structure more visible: res = [ ( inner + ["new value"] if inner[0] == "blah" else inner ) for inner in outer ] Maybe. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
On Thursday, March 2, 2017 at 2:57:02 PM UTC-5, Jussi Piitulainen wrote: > Peter Otten <__pete...@web.de> writes: > > > Andrew Zyman wrote: > > > >> On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: > >>> Andrew Zyman wrote: > >>> . > >>> . > >>> > End result: > >>> > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] > >>> > >>> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > >>> >>> for inner in outer: > >>> ... if inner[0] == "blah": > >>> ... inner.append("new value") > >> > >> thank you. this will do. > >> Just curious, is the above loop can be done in a one-liner? > > > > Ah, that newbie obsession ;) > > > outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > [inner + ["new value"] if inner[0] == "blah" else inner for inner in > > outer] > > [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > > > > Note that there is a technical difference to be aware of -- matching > > lists are replaced rather than modified. > > I take it you are too sane, or too kind, to suggest the obvious > solution: > > >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > >>> [inner.append("new value") for inner in outer if inner[0] == "blah"] > [None] > >>> outer > [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > > [snip] Arh!!! this is it :) I'm sure i'll regret this line of code in 2 weeks - after i successfully forget what i wanted to achieve :) -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
Peter Otten <__pete...@web.de> writes: > Andrew Zyman wrote: > >> On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: >>> Andrew Zyman wrote: >>> . >>> . >>> > End result: >>> > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] >>> >>> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >>> >>> for inner in outer: >>> ... if inner[0] == "blah": >>> ... inner.append("new value") >> >> thank you. this will do. >> Just curious, is the above loop can be done in a one-liner? > > Ah, that newbie obsession ;) > outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] [inner + ["new value"] if inner[0] == "blah" else inner for inner in > outer] > [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] > > Note that there is a technical difference to be aware of -- matching > lists are replaced rather than modified. I take it you are too sane, or too kind, to suggest the obvious solution: >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >>> [inner.append("new value") for inner in outer if inner[0] == "blah"] [None] >>> outer [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] [snip] -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
Andrew Zyman wrote: > On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: >> Andrew Zyman wrote: >> . >> . >> > End result: >> > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] >> >> >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >> >>> for inner in outer: >> ... if inner[0] == "blah": >> ... inner.append("new value") > > thank you. this will do. > Just curious, is the above loop can be done in a one-liner? Ah, that newbie obsession ;) >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >>> [inner + ["new value"] if inner[0] == "blah" else inner for inner in outer] [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] Note that there is a technical difference to be aware of -- matching lists are replaced rather than modified. >> While this is what you are asking for it will get slower as the list >> grows. A better solution uses a dictionary: > >> >>> lookup = {inner[0]: inner[1:] for inner in outer} > > Yes, understood, i don't expect the list to grow above a few thousand > entries. But i do agree that utilizing the dict is more efficient. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
On Thursday, March 2, 2017 at 11:27:34 AM UTC-5, Peter Otten wrote: > Andrew Zyman wrote: > . > . > > End result: > > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] > > >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] > >>> for inner in outer: > ... if inner[0] == "blah": > ... inner.append("new value") thank you. this will do. Just curious, is the above loop can be done in a one-liner? > While this is what you are asking for it will get slower as the list grows. > A better solution uses a dictionary: > >>> lookup = {inner[0]: inner[1:] for inner in outer} Yes, understood, i don't expect the list to grow above a few thousand entries. But i do agree that utilizing the dict is more efficient. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of the lists - append after search
Andrew Zyman wrote: > Hello, > please advise. > > I'd like search and append the internal list in the list-of-the-lists. > > Example: > ll =[ [a,1], [b,2], [c,3], [blah, 1000] ] > > i want to search for the internal [] based on the string field and, if > matches, append that list with a value. > > if internal_list[0] == 'blah': > ll[ internal_list].append = [ 'new value'] > > End result: > ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >>> for inner in outer: ... if inner[0] == "blah": ... inner.append("new value") ... >>> outer [['a', 1], ['b', 2], ['c', 3], ['blah', 1000, 'new value']] While this is what you are asking for it will get slower as the list grows. A better solution uses a dictionary: >>> outer = [["a", 1], ["b", 2], ["c", 3], ["blah", 1000]] >>> lookup = {inner[0]: inner[1:] for inner in outer} >>> lookup {'a': [1], 'blah': [1000], 'b': [2], 'c': [3]} >>> lookup["blah"].append("whatever") >>> lookup {'a': [1], 'blah': [1000, 'whatever'], 'b': [2], 'c': [3]} -- https://mail.python.org/mailman/listinfo/python-list
list of the lists - append after search
Hello, please advise. I'd like search and append the internal list in the list-of-the-lists. Example: ll =[ [a,1], [b,2], [c,3], [blah, 1000] ] i want to search for the internal [] based on the string field and, if matches, append that list with a value. if internal_list[0] == 'blah': ll[ internal_list].append = [ 'new value'] End result: ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ] I came up with the following, but the second stmnt is not correct: print [x for x in ll if x[0]== 'blah'] print ll.index([x for x in ll if x[0]=='blah']) output: ValueError: [['blah', 1]] is not in list thank you AZ -- https://mail.python.org/mailman/listinfo/python-list