how to construct a list of only one tuple

T

TP

Hi,

If I do:
a=("1","2")
b=[("3","4"),("5","6")]
list(a)+b
['1', '2', ('3', '4'), ('5', '6')]

I would like rather to obtain:

[('1', '2'), ('3', '4'), ('5', '6')]

Am I compelled to do:
[('1', '2'), ('3', '4'), ('5', '6')]

Thanks

Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
T

TP

a=("1","2")
b=[("3","4"),("5","6")]
list(a)+b
['1', '2', ('3', '4'), ('5', '6')]
a = ("1", "2")
b = [("3", "4"), ("5", "6")]
[a] + b
[('1', '2'), ('3', '4'), ('5', '6')]

Thanks a lot.
Why this difference of behavior between list(a) and [a]?

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
D

Diez B. Roggisch

TP said:
a=("1","2")
b=[("3","4"),("5","6")]
list(a)+b
['1', '2', ('3', '4'), ('5', '6')]
a = ("1", "2")
b = [("3", "4"), ("5", "6")]
[a] + b
[('1', '2'), ('3', '4'), ('5', '6')]

Thanks a lot.
Why this difference of behavior between list(a) and [a]?

Because they are different things.


list is a function that takes an iterable & creates a list out of it.

The [...] syntactic form takes a list of arguments and returns a list
composed of them.

You see the difference maybe with this:


a = 1

foo = [a]
bar = list(a)

->

Traceback (most recent call last):
File "/tmp/test.py", line 5, in <module>
bar = list(a)
TypeError: 'int' object is not iterable

Diez
 
C

Chris Rebert

a=("1","2")
b=[("3","4"),("5","6")]
list(a)+b
['1', '2', ('3', '4'), ('5', '6')]
a = ("1", "2")
b = [("3", "4"), ("5", "6")]
[a] + b
[('1', '2'), ('3', '4'), ('5', '6')]

Thanks a lot.
Why this difference of behavior between list(a) and [a]?

Because list() is used to convert other iterable types into lists
(e.g. list("abc") ==> ['a','b','c'], list((c,d)) ==> [c,d],
list(set(x,y,z)) ==> [y, z, x]).
In contrast, a list literal just constructs a list. This makes sense
because the behavior of list() is useful and you can just use the
literal syntax instead for cases such as yours.
In Python 3.0, I believe you'll be able to use the splat operator to
do e.g. [a, *b] to get your desired result.

For reference:
list(a) <==> [x for x in a]
[a,b,c] <==> list((a,b,c))

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
M

MRAB

TP said:
a=("1","2")
b=[("3","4"),("5","6")]
list(a)+b
['1', '2', ('3', '4'), ('5', '6')]
a = ("1", "2")
b = [("3", "4"), ("5", "6")]
[a] + b
[('1', '2'), ('3', '4'), ('5', '6')]

Thanks a lot.
Why this difference of behavior between list(a) and [a]?
list(a) iterates through its argument, building a list from the results.
In this case it iterates though the items in the tuple.

[a] just creates a list with a as an item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top