Questions tagged [pep448]

Questions related to PEP 448: Additional Unpacking Generalizations, used for unpacking iterables and mappings within literal tuples, lists, sets and dicts using * and **

For questions related to PEP 448: Additional Unpacking Generalizations, used for unpacking iterables and mappings within literal tuples, lists, sets and dicts

6 questions
32
votes
3 answers

Python: Splat/unpack operator * in python cannot be used in an expression?

Does anybody know the reasoning as to why the unary (*) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File…
Har
  • 3,727
  • 10
  • 41
  • 75
20
votes
1 answer

asterisk in tuple, list and set definitions, double asterisk in dict definition

I'm playing now with Python 3.5 interpreter and found very interesting behavior: >>> (1,2,3,"a",*("oi", "oi")*3) (1, 2, 3, 'a', 'oi', 'oi', 'oi', 'oi', 'oi', 'oi') >>> [1,2,3,"a",*range(10)] [1, 2, 3, 'a', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>…
George Sovetov
  • 4,942
  • 5
  • 36
  • 57
3
votes
2 answers

Weird unpacking in list comprehension

I was watching a lecture from David Beazley. At minute 23:20 he does some "magic" with unpacking that I am having hard time understanding. The "magic line" is fail = [ { **row, 'DBA Name': row['DBA Name'].replace("'",'').upper() } for row in fail…
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
1
vote
0 answers

What does the * sign mean in the following code?

I was solving a programming puzzle and came across this solution by someone. I don't understand why the * is used. a = [*map(len,input().replace(" ","").replace("12","1 2").replace("21","2 1").split(" "))]
govind
  • 11
  • 2
1
vote
2 answers

Additional Unpacking Generalizations (PEP 448) with variable number of elements

The acceptance of PEP 448 has introduced Additional Unpacking Generalizations in Python 3.5. For example: >>> l1 = [1, 2, 3] >>> l2 = [4, 5, 6] # unpack both iterables in a list literal >>> joinedList = [*l1, *l2] >>> print(joinedList) [1, 2, 3, 4,…
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
0
votes
0 answers

Whats the difference between * and list() in Python?

I have noticed that some people use * to turn something into a list. I initially only knew of putting list(x) around the object, but doing *x also seems to work. Are they equivalent? For example, both *Counter(list).values() and…
user832075
  • 15
  • 6