How do you check if an element appears twice in a list python?

Without importing another function check if a list contains two of the same element

F.E list = [3,4,5,3] since 3 appears twice in the list return True

Thanks for the help

asked May 6, 2020 at 21:31

You can convert to set and check the resulting length:

len[set[data]] < len[data]

answered May 6, 2020 at 21:35

a_guesta_guest

31.8k9 gold badges55 silver badges106 bronze badges

If you need to check if the element occurs exactly twice, you could do this:

l = [3,4,5,3]
if 2 in set[[l.count[n] for n in l]]:
    print['True']

answered May 6, 2020 at 21:39

ssharmassharma

1511 silver badge3 bronze badges

There are different approaches to solve this problem.

Solution 1: Use set[]

if len[set[]]=2 for x in lst] Will be true if at least 1 item in lst appears twice or more in string.

Kirill Vidov Something like this: s = 'Sololearn' l = ['a', 'o', 'l', 'r', 'n'] print[[x for x in l if s.count[x] >= 2]] # will only print 'o' and 'l'

Thank you everyone for the help

Kirill Vidov Show ur attempt

Kuba Siekierzyński can you please help if you can?

Kirill Vidov Ask if you have any doubt in it a=[1,2,3,"a",5,"b",4.8] print[*[i for i in a if type[i]==type[""]]] Returns those elements from the list which are in string

Exactly, I would use count[] to determine if there is more than one occurence of an element in a collection.

Abhay if any[x in string for x in list] >= 2, but that doesn't work

Questions : Check if an certain element appears twice in a list - Python

2022-10-02T13:15:46+00:00 2022-10-02T13:15:46+00:00

940

Without importing another function check if anycodings_python-3.x a list contains two of the same element

F.E list = [3,4,5,3] since 3 appears twice anycodings_python-3.x in the list return True

Thanks for the help

Total Answers 3

31

Answers 1 : of Check if an certain element appears twice in a list - Python

You can convert to set and check the anycodings_list resulting length:

len[set[data]] < len[data]

0

2022-10-02T13:15:46+00:00 2022-10-02T13:15:46+00:00Answer Link

mRahman

3

Answers 2 : of Check if an certain element appears twice in a list - Python

If you need to check if the element anycodings_list occurs exactly twice, you could do this:

l = [3,4,5,3]
if 2 in set[[l.count[n] for n in l]]:
    print['True']

0

2022-10-02T13:15:46+00:00 2022-10-02T13:15:46+00:00Answer Link

joy

3

Answers 3 : of Check if an certain element appears twice in a list - Python

There are different approaches to solve anycodings_list this problem.

Solution 1: Use set[]

if len[set[]]

Chủ Đề