2

I am learning python, now, i came across a code snippet which looks like this:

my_name={'sujit','amit','ajit','arijit'}
for i, names in enumerate(my_name):
    print "%s" %(names[i])

OUTPUT

s
m
i
t

But when I modify the code as:

my_name=['sujit','amit','ajit','arijit']
for i, names in enumerate(my_name):
    print "%s" %(names[i])

OUTPUT

s
m
i
j

What is the difference between {} and []? The [] is giving me the desired result for printing the ith character of the current name from the list. Bu the use of {} is not.

4 Answers 4

4

{} creates a set, whereas [] creates a list. The key differences are:

  • the list preserves the order, whereas the set does not;
  • the list preserves duplicates, whereas the set does not;
  • the list can be accessed through indexing (i.e. l[5]), whereas the set can not.

The first point holds the key to your puzzle. When you use a list, the loop iterates over the names in order. When you're using a set, the loop iterates over the elements in an unspecified order, which in my Python interpreter happens to be sujit, amit, arijit, ajit.

P.S. {} can also be used to create a dictionary: {'a':1, 'b':2, 'c':3}.

Sign up to request clarification or add additional context in comments.

2 Comments

But could you please explain the internal logic behind the last line of the first output set? i mean the t
It's the 't' from "ajit". Since sets don't preserve order, you are getting a random order out of enumerate. I would guess that the order coming out is ['sujit','amit','arijit','ajit']. That would make the first letter the 's' of 'sujit', the second letter the 'm' of 'amit', the third the 'i' of 'arijit' and the fourth the 't' of 'ajit'.
3

The {} notation is set notation rather than list notation. That is basically the same as a list, but the items are stored in a jumbled up order, and duplicate elements are removed. (To make things even more confusing, {} is also dictionary syntax, but only when you use colons to separate keys and values -- the way you are using it, is a set.)

Secondly, you aren't using enumerate properly. (Or maybe you are, but I'm not sure...)

enumerate gives you corresponding index and value pairs. So enumerate(['sujit','amit','ajit','arijit']) gives you:

[(0, 'sujit'), (1, 'amit'), (2, 'ajit'), (3, 'arijit')]

So this will get you the first letter of "sujit", the second letter of "amit", and so on. Is that what you wanted?

2 Comments

(And it will crash as soon as there is a word in the list whose index is greater than or equal to its length, which is why I said I don't think it's being used properly.)
Yes. I tried to modify the last element arijit to art and the code crashed.
0

{} do not enclose a list. They do not enclose any kind of sequence; they enclose (when used this way) a set (in the mathematical sense). The elements of a set do not have a specified order, so you get them enumerated in whatever order Python put them in. (It does this so that it can efficiently ensure the other important constraint on sets: they cannot contain a duplicate value).

This is specific to Python 3. In 2.x, {} cannot be used to create a set, but only to create a dict. (This also works in Python 3.) To do this, you specify the key-value pairs separated by colons, thus: {'sujit': 'amit', 'ajit': 'arijit'}.

(Also, a general note: if you say "question" instead everywhere that you currently say "doubt", you will be wrong much less often, at least per the standards of English as spoken outside of India. I don't particularly understand how the overuse of 'doubt' has become so common in English as spoken by those from India, but I've seen it in many places across the Internet...)

1 Comment

Python 2.7 supports {} for set
0

sets do not preserve order:

[] is a list:

>>> print ['sujit','amit','ajit','arijit']
['sujit', 'amit', 'ajit', 'arijit']

{} is a set:

>>> print {'sujit','amit','ajit','arijit'}
set(['sujit', 'amit', 'arijit', 'ajit'])

So you get s,m,i,j in the first case; s,m,i,t in the second.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.