Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't [[]] == list(list()) [duplicate]

Tags:

python

list

I just randomly observed the following in Python and am curious to know the reason why this evaluates to False:

list(list()) == [[]]

Interestingly if you rearrange the syntax in the following ways, the first way evaluates as False, but the second as True:

list([]) == [[]]
[list()] == [[]]

What is going on under the hood here?

like image 954
Toby Petty Avatar asked Nov 21 '19 20:11

Toby Petty


People also ask

Does list accept duplicate?

List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value. List permits any number of null values in its collection while Set permits only one null value in its collection.

Which list will not allow duplicates?

Duplicates : ArrayList allows duplicate values while HashSet doesn't allow duplicates values.


1 Answers

list does not construct a list that contains its argument; it constructs a list whose elements are contained in its argument. list([]) does not return [[]]; it returns []. Thus, list(list()) == list([]) == [].

like image 71
chepner Avatar answered Nov 15 '22 16:11

chepner