PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] why duplicates removal in list using a set method gives output with different index each time?

 November 07, 2022     list, python, set     No comments   

Issue

I know to remove duplicates in a list...just curious to know why set does not give order as orginal list

my_list = ['apple', 'mango', 'grape', 'apple', 'guava', 'pumpkin']
>>>[*set(my_list)]

#output:
>>> ['mango', 'apple', 'grape', 'guava', 'pumpkin']
>>> ['pumpkin', 'guava', 'grape', 'mango', 'apple']

Solution

As all the comments say, a set is unordered, always.

But internally it uses a hash table, and IIRC the values stored are the hash of the object modulo the table size. Now small integers tend to have themselves as their hash values, so you may have the impression that they are sorted (not ordered by insertion order), but this won't always be the case:

ls = [1,2,3]
[*set(ls)]
[1, 2, 3]

ls = [2,1,3]
[*set(ls)]
[1, 2, 3]

ls2=[-1,-2,3]
[*set(ls2)]
[3, -1, -2]

ls2=[-2,-1,3]
[*set(ls2)]
[3, -2, -1]

Other objects, like the strings in your example, have very different hash values, so the behaviour is totally different:

hash('mango')
-7062263298897675226


Answered By - gimix
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing