1

In python the output is differ when i use {} instead of [] to holding the values into a variable.

In my program the variable is declared as "numbers = []" then the sum of the values is 48 if i declared as "numbers = {}" then the output is 39

numbers = {6, 5, 3, 8, 4, 2, 5, 4, 11}
sum = 0
for val in numbers:
    sum = sum+val
print("The sum is", sum)

I expect the output of sum is 48,but the actual output is 39.

4 Answers4

4

'[]' creates a list while '{}' creates a set if not used as a key-value pair. Sets have a property to remove duplicate elements. So on using '{}', you numbers set is automatically converted to numbers = {6, 5, 3, 8, 4, 2, 11} removing two elements i.e 5 and 4. Hence the total is 39.

Trollsors
  • 492
  • 4
  • 17
3

so the {} notation is used in python to represent sets. if you output the values of the variable after both type of assignments, you will be able to understand the difference

# Set Initialisation
>>> numbers = {6, 5, 3, 8, 4, 2, 5, 4, 11}
>>> print(numbers)
{2, 3, 4, 5, 6, 8, 11}
>>> print(type(numbers))
<class 'set'>

# Array Initialisation
>>> numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
>>> print(numbers)
[6, 5, 3, 8, 4, 2, 5, 4, 11]
>>> print(type(numbers))
<class 'list'>

So in the set case you will loose the duplicate elements.

Asnim P Ansari
  • 1,932
  • 1
  • 18
  • 41
1

{} is a set. Sets have the property of only unique values, so there are no duplicates.

If you notice, you have the number 5 and the number 4 repeating, but each of those will only be put into the set once.

5 + 4 = 9

48 - 9 = 39

On the other hand, [] are lists and can store duplicate values.

If you wrote a dictionary instead of a set:

{
  0: 6,
  1: 5,
  2: 3,
  3: 8,
  4: 4,
  5: 2,
  6: 5,
  7: 4,
  8: 11
}

And then iterated over the values, you would get the result you are expecting.

pickle
  • 855
  • 1
  • 7
  • 16
1

You are creating a set below:

numbers = {6, 5, 3, 8, 4, 2, 5, 4, 11}

whereas the below one will create a list:

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

The difference is that, set datastructure in python is equivalent to one in mathematics, where elements are unordered and unindexed. No duplicates are allowed.

Incase of a list, however, elements are ordered, indexed and duplicates are allowed.This is the reason, why you see difference in output for the program you have written.

You can always check the type of an object by using type function.

In the below code snippet, you can see that a set is created and the elements in it.

>>> numbers = {6, 5, 3, 8, 4, 2, 5, 4, 11}
>>> type(numbers)
<type 'set'>
>>> print numbers
set([2, 3, 4, 5, 6, 8, 11])

Code snippet for list below:

>>> numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
>>> type(numbers)
<type 'list'>
>>> print numbers
[6, 5, 3, 8, 4, 2, 5, 4, 11]

In general, notations like {} for set and [] for list are very useful when used with comprehension techniques. Though they can very well be used the way you have done, you can also use set() or list() to make the code a little more readable for Python beginners.

Jay
  • 24,173
  • 25
  • 93
  • 141