How can i get the common elements in the first two list and save to a new list in python 3?

Python_beginner
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!

Sanjeevan Khanduri

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How i can iterate list to get 10 elements each time in java

분류에서Dev

How can I create a table of only the unique elements in a list so I can order the elements in terms of frequency?

분류에서Dev

How to removes all the elements between the two elements that I select in linked list

분류에서Dev

Scala: How to create a new list from operation on adjacent elements of a list

분류에서Dev

How do I get a list of programs that can be installed?

분류에서Dev

How I can get list item values when I click on list view

분류에서Dev

Unicode elements in list save to file

분류에서Dev

How to get only specific elements of list in racket

분류에서Dev

How can I make a Python function examine every element of a list?

분류에서Dev

python : save dictionary into list

분류에서Dev

How to count the repetition of the elements in a list python, django

분류에서Dev

In python how to count occurance of elements in list

분류에서Dev

In python 3, how can I put individual bytes from a bytes object into a list without them being converted to integers?

분류에서Dev

How can I use Selenium (Python) to do a Google Search and then open the results of the first page in new tabs?

분류에서Dev

Add elements in a list Python

분류에서Dev

How can I get pygame for Python3?

분류에서Dev

How do i get id of autocomplete textview out of two autocomplete textview on item selected from its list?

분류에서Dev

How can I extract list from list in prolog?

분류에서Dev

How can I recreate a list of list of floats from the contents of a file?

분류에서Dev

(discord.py) How can I get a list of how many permissions a user has

분류에서Dev

How can I create List of rectangles in TypeScript?

분류에서Dev

How can I centralize a list with css?

분류에서Dev

How can I create a resource list on Apigility?

분류에서Dev

How can I return a list of summary statistics?

분류에서Dev

How do I get the level of depth of a list?

분류에서Dev

How can write a python function that takes two parameters one being a list

분류에서Dev

IBM Filenet P8 : How can i get the localized display names of choice list items

분류에서Dev

Use Parse.com and Android, how can I get a list of Games that a pair of Players share?

분류에서Dev

How can I get the list of the currently installed certificate authority on my ubuntu?

Related 관련 기사

  1. 1

    How i can iterate list to get 10 elements each time in java

  2. 2

    How can I create a table of only the unique elements in a list so I can order the elements in terms of frequency?

  3. 3

    How to removes all the elements between the two elements that I select in linked list

  4. 4

    Scala: How to create a new list from operation on adjacent elements of a list

  5. 5

    How do I get a list of programs that can be installed?

  6. 6

    How I can get list item values when I click on list view

  7. 7

    Unicode elements in list save to file

  8. 8

    How to get only specific elements of list in racket

  9. 9

    How can I make a Python function examine every element of a list?

  10. 10

    python : save dictionary into list

  11. 11

    How to count the repetition of the elements in a list python, django

  12. 12

    In python how to count occurance of elements in list

  13. 13

    In python 3, how can I put individual bytes from a bytes object into a list without them being converted to integers?

  14. 14

    How can I use Selenium (Python) to do a Google Search and then open the results of the first page in new tabs?

  15. 15

    Add elements in a list Python

  16. 16

    How can I get pygame for Python3?

  17. 17

    How do i get id of autocomplete textview out of two autocomplete textview on item selected from its list?

  18. 18

    How can I extract list from list in prolog?

  19. 19

    How can I recreate a list of list of floats from the contents of a file?

  20. 20

    (discord.py) How can I get a list of how many permissions a user has

  21. 21

    How can I create List of rectangles in TypeScript?

  22. 22

    How can I centralize a list with css?

  23. 23

    How can I create a resource list on Apigility?

  24. 24

    How can I return a list of summary statistics?

  25. 25

    How do I get the level of depth of a list?

  26. 26

    How can write a python function that takes two parameters one being a list

  27. 27

    IBM Filenet P8 : How can i get the localized display names of choice list items

  28. 28

    Use Parse.com and Android, how can I get a list of Games that a pair of Players share?

  29. 29

    How can I get the list of the currently installed certificate authority on my ubuntu?

뜨겁다태그

보관