2

Trying to adapt further from this question on stackoverflow:

How to convert a set to a list in python?

I have been trying to solve this riddle on interactivepython.org The riddle is exactly on the end of the page and it goes like...

We have a list, for eg list1 = ['cat','dog','rabbit'] and now using list comprehension (strictly list comprehension), we have to create a list of each alphabet in the each word in list1 and the resulting list should not contain duplicates.

So the expected answer is something like:

['c', 'a', 't', 'd', 'o', 'g', 'r', 'b', 'i']

Its alright even if the sequence is not maintained.

Now first to create a list of all characters, used:

print [word[i] for word in list1 for i in range(len(word))]

and it gives output

['c', 'a', 't', 'd', 'o', 'g', 'r', 'a', 'b', 'b', 'i', 't']

This includes duplicates as well

so I then created this into set as sets do not contain duplicates:

print set([word[i] for word in list1 for i in range(len(word))])

output:

set(['a', 'c', 'b', 'd', 'g', 'i', 'o', 'r', 't'])

however this returns a set not a list and the same can be verified by:

type(set([word[i] for word in list1 for i in range(len(word))]))

output:

<type 'set'>

Now, in the video on the above given link of interactivepython.org the guy just puts encloses the entire thing after print in list() as follows:

print list(set([word[i] for word in list1 for i in range(len(word))]))

and he gets the result output in list, however I don't get the same when I try using idle that uses python 2.7.8. It instead gives me an error:

Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    print list(set([word[i] for word in list1 for i in range(len(word))]))
TypeError: 'list' object is not callable

I think this is may be because interactive python uses Python 3 for its tutorials. So is this merely a difference between Python 3 and Python 2.7?

Also, how can I achieve similar output using Python 2.7.8

Thanks

Deep
  • 1,151
  • 11
  • 18
  • 1
    I'd just go for `list(set().union(*list1))` if that's not completely against the rules - that'll work on 2.x and 3.x – Jon Clements Sep 30 '14 at 05:06

2 Answers2

2

We can use list(set) to convert set to list in python 2.7

>>> a=set([1,2,3])
>>> a
set([1, 2, 3])
>>> b=list(a)
>>> b
[1, 2, 3]
>>> 

I think python 3 is also having same features

Arundas R
  • 770
  • 7
  • 20
0

All Python versions support creating a list via the syntax list(<iterable>), so your specific Python version is not the cause of the error.

I think you've done a similar mistake to that in the accepted answer to the linked question you provided, i.e. in your interactive shell you defined a variable with the name list which shadows the built-in type list.

For example, on Python 2.7.6:

>>> list({1, 2, 3}) # successfully convert a set to a list
[1, 2, 3]
>>> list = []       # shadow the built-in type list
>>> list({1, 2, 3}) # this fails since list no longer references the built-in type
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

Try exiting your shell and re-entering it, executing the same commands, and see if they work.


On an unrelated note, you can further improve your list generation from this:

[word[i] for word in list1 for i in range(len(word))]

to this:

[letter for word in list1 for letter in word]

since iterating over a string returns its characters one by one. In fact, this is also the underlying logic behind @JonClements astute solution:

list(set().union(*list1))
Community
  • 1
  • 1
Yoel
  • 9,144
  • 7
  • 42
  • 57