Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 List and loop
#1
I am not getting expected output? Why?

phrase="don't panic"
plist=list(phrase)
str1="on tap"
qlist=list(str1)
for item in qlist:
    if item not in plist:
        plist.remove(item)
print(plist)

Output:
Output:
['o', 'n', 't', ' ', 'p', 'a', 'n', 'c']
Desired output:
I want program to remove characters from plist which are not in qlist list.
Output:
['o', 'n', “‘”, 't','p', ‘a’, ‘n’]
buran wrote Oct-23-2019, 05:53 AM:
Please use square brackets for tags - it's BBcode, not HTML
Quote
#2
(Oct-23-2019, 05:51 AM)Sandeep2000 Wrote: I am not getting expected output? Why?
define expected output in plain english, please
Quote
#3
If you want items in both lists, use set union https://www.programiz.com/python-programming/set#union
Quote
#4
(Oct-23-2019, 07:27 AM)woooee Wrote: If you want items in both lists, use set union https://www.programiz.com/python-programming/set#union
Thank you for suggesting better ways. I know there are many ways to do this simple task.
But can i expect that output from code above? Can you see if there is something wrong with the code itself.

Sorry i misplaced plist and qlist.

phrase="don't panic"
plist=list(phrase)
str1="on tap"
qlist=list(str1)
for item in plist:
    if item not in qlist:
        plist.remove(item)
print(plist)

Quote
#5
Sandeep2000 Wrote:Can you see if there is something wrong with the code itself.
Yes something is fundamentally wrong in this code: Don't remove items in a python list inside a loop that's iterating on this list. To avoid this, try for item in list(plist), which iterates on a copy of the initial list.
Quote
#6
Main problem with your code is that you mutate the list while iterating over it (it can be done but you must know and understand what are you doing):

Quote:"If fact, in any programming language for most part if you mutate something while you iterating over it you living in state of sin and you deserve whatever happens to you" -- Raymond Hettinger, Python core-developer, Transforming Code into Beautiful, Idiomatic Python

If your objective is: 'give me list of all chars in phrase which are in str1' then one can skip all conversions etc and have list comprehension:

>>> phrase="don't panic"
>>> str1="on tap"
>>> [char for char in phrase if char in str1]
['o', 'n', 't', ' ', 'p', 'a', 'n']
newbieAuggie2019 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  loop through list or double loop 3Pinter 4 860 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 999 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 1,651 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 3,295 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)