list1 = [input("Enter the values for the first list: ")]
list2 = [input("Enter the values for the second list: ")]
print(list1)
print(list2)
list3 = []
for element in list1:
if element in list2:
list3 = list2.append(element)
print(list3)
This is what i have tried. but I am getting an empty list as list3!
You can't use [input('Enter numbers: ')]
to get the numbers for a list. This will create a list containing the input string. What you really have to do is first take input for numbers in a variable, lets say list1_inp
and then split list1_inp
based on spaces using nums = list1_inp.split(' ')
. Now you can iterate through your list checking for common elements.
list1_inp = input('Enter the elements separated by spaces : ')
nums = list1_inp.split()
list2_inp = input('Enter the elements separated by spaces : ')
nums2 = list2_inp.split()
temp = nums2
final = []
for elem in nums:
if elem in temp:
final.append(elem)
temp.remove(elem)
print(final)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다