Question

How to input a list from user def SameIntheList(): list1 = list(input("Enter list1:")) list2 = list(input("Enter...

How to input a list from user

def SameIntheList():

list1 = list(input("Enter list1:"))

list2 = list(input("Enter list2:"))

same_in_list = []

for i in list1:

if i in list2:

same_in_list.append(i)

print(same_in_list)

SameIntheList()

-----------------

Enter list1:a,b,c

Enter list2:a,b,g

['a', ',', 'b', ',']

----------------

I just want to display the same character in the 2 list

['a', 'b']

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey,Hey,Hey!!

Let me tell you where you are going wrong.

You are taking input using list function. And while giving input you are giving input with commas.

The thing is list function takes input without any commas and spaces.And if you want to give input a list with commas or spaces ,you will have to use split function.

Your code is showing commas(,) in output because you have input commas in both list and your code is treating  commas as character like a,b,c.

So Commas are there in both list that is why it is appearing in the output.

Okay,Now lets see the correct program code:-

Here is the code:

OUTPUT:-

Voila!! We did it!!

Please give it a thumbs up if you liked my answer.

Add a comment
Know the answer?
Add Answer to:
How to input a list from user def SameIntheList(): list1 = list(input("Enter list1:")) list2 = list(input("Enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to remove all occurrences of 2 from list2. Then display the list. Python import random...

    How to remove all occurrences of 2 from list2. Then display the list. Python import random list1 = [] for i in range (10): list1.append(random.randint(1,4)) print('List 1:') print(list1) list2 = [] list2.append(list1[5:]) print('List 2:') print(list2) list2.remove ******** (This is the part I am having trouble on!!) print('List 2 with all 2s removed:') print(list2) Sample output: List 1: [4, 1, 1, 3, 3, 3, 2, 4, 2, 4] List2: [3, 2, 4, 2, 4] List 2 with all 2s removed: [3,...

  • Python Assignment: def sliceNdice(list2, s,e): L = len(list2) if(s>0 and s<e and e+s<=L): list1 = list2[s:e+s]...

    Python Assignment: def sliceNdice(list2, s,e): L = len(list2) if(s>0 and s<e and e+s<=L): list1 = list2[s:e+s] else: list1 = [ ] return list1 refer to the code above. What is the value of L after line 2 and we call: x = sliceNdice([3,6,9,7] 2,6) 3. Refer to the previous question. What is happening at line 4? 4. For the code shown below: for num in range (1,4): print(num) What is the first value printed

  • Can someone fix this python program? I'm trying to do three things: Create a function that...

    Can someone fix this python program? I'm trying to do three things: Create a function that takes in a string as a parameter. Then create a dictionary of characters to integers, where the integer represents how many times the number of times the character appears in the word. Create a function that takes two lists as parameters and returns the elements they have in common of the two lists. Ask the user to create a list of numbers and keep...

  • Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as o...

    Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as output List2. List2 contains all items that appear in List1, except that repeated or duplicate items are not present in List1. For example: ?- remdup([1,2,3] ,x) ?- remdup([3,1,2,31,x) X=[1, 2, 3]. Note that the order of items in List2 is unspecified: for instance, [3,1,2 is an acceptable result for the second example above. 4. 2 marks Consider a course...

  • Something is preventing this python code from running properly. Can you please go through it and...

    Something is preventing this python code from running properly. Can you please go through it and improve it so it can work. The specifications are below the code. Thanks list1=[] list2=[] def add_player(): d={} name=input("Enter name of the player:") d["name"]=name position=input ("Enter a position:") if position in Pos: d["position"]=position at_bats=int(input("Enter AB:")) d["at_bats"] = at_bats hits= int(input("Enter H:")) d["hits"] = hits d["AVG"]= hits/at_bats list1.append(d) def display(): if len(list1)==0: print("{:15} {:8} {:8} {:8} {:8}".format("Player", "Pos", "AB", "H", "AVG")) print("ORIGINAL TEAM") for x...

  • Python. Write a function swap_lists that takes in two lists and swap their elements in place....

    Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...

  • for python-3 I want to prompt the user to enter their first name and then Call...

    for python-3 I want to prompt the user to enter their first name and then Call the is_field_blank function to see if nothing was entered.and If nothing was entered i want it to display the following message "First Name must be Entered" and then re-prompt the user to enter the first name. I want it to do this repeatedly until the user enters a first name. this is what i have so far def main():     yourFirst = input("What is...

  • Deletion of List Elements The del statement removes an element or slice from a list by position....

    Deletion of List Elements The del statement removes an element or slice from a list by position. The list method list.remove(item) removes an element from a list by it value (deletes the first occurance of item) For example: a = ['one', 'two', 'three'] del a[1] for s in a: print(s) b = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'] del b[1:5] print(b) b.remove('a') print(b) # Output: ''' one three ['a', 'f', 'a', 'b'] ['f', 'a', 'b'] ''' Your textbook...

  • in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Po...

    in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Pop(self): return self.items.pop() def Display(self): for i in reversed(self.items): print(i,end="") print() s = My_Stack() #taking input from user str = input('Enter your string for palindrome checking: ') n= len(str) #Pushing half of the string into stack for i in range(int(n/2)): s.Push(str[i]) print("S",end="") s.Display() s.Display() #for the next half checking the upcoming string...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT