Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
itertools and chain
#1
Hi,

Can someone please help me understand what is the difference between this t = list(itertools.chain(*([1,2,3],[4,5,6]))) and y = list(itertools.chain(([1,2,3],[4,5,6]))).

The first one gives this result [1,2,3,4,5,6] whilst the second one gives a list of lists i.e. [[1,2,3],[4,5,6]]
Reply
#2
That's the difference!
Reply
#3
* take function argument and expands it into actual positional arguments in the function call.
def bar(seq, a=None, b=None):
    print(seq, a, b)

>>> bar([1, 2, 3])
[1, 2, 3] None None

>>> bar(*[1, 2, 3])
1 2 3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Chain object that have parent child relation.. SpongeB0B 10 1,071 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  itertools and amazing speed Pedroski55 8 2,035 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  Find if chain of characters or number Frankduc 4 1,789 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,378 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,230 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,075 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,830 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,678 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,153 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,800 Jun-10-2019, 02:41 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020