Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:30:45+00:00 2026-05-31T21:30:45+00:00

I am having some trouble getting expected results out of the zip command. Cases

  • 0

I am having some trouble getting expected results out of the zip command. Cases 1-3 make sense, but in cases 4 and 5 (which I assume are equivalent?) I expect the results to be [[‘a’],[‘b’],[‘c’],[‘d’]], but instead the entirety of the second list is appended to each sublist of the list of lists I initialize.

Case 1:

>>> for a in zip([1,2,3,4],['a','b','c','d']):
...     print a

(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

Case 2:

>>> for (a,b) in zip([1,2,3,4],['a','b','c','d']):
...     print a,b
... 
1 a
2 b
3 c
4 d

Case 3:

>>> temp = [[]] * 4
>>> for (a,b) in zip([0,1,2,3],['a','b','c','d']):
...     temp[a] = b
... 
>>> temp
['a', 'b', 'c', 'd']

Case 4:

>>> temp = [[]] * 4
>>> for (a,b) in zip([0,1,2,3],['a','b','c','d']):
...     temp[a].append(b)
... 
>>> temp
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]

Case 5:

>>> temp = [[]] * 4
>>> for a,b in zip([0,1,2,3],['a','b','c','d']):
...     temp[a].append(b)
... 
>>> temp
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:30:47+00:00Added an answer on May 31, 2026 at 9:30 pm

    You’ve run into the way Python deals with variable names and references. It can be confusing if you have worked with C-style languages before, but makes a lot of sense if you don’t think that way!

    Python has “things” and “names for things”. For example, x is a name, [] is a thing, and x=[] assigns the name x to the thing [].

    The list multiplication syntax is a bit confusing with regards to names and things. Let’s expand it out to see how it works:

    name = [x]*4
    

    is the same as

    _temp = x
    name = [_temp, _temp, _temp, _temp]
    

    With that in mind, the following code should make sense:

    >>> x = []
    >>> y = [x]*4
    >>> y[0].append(1)
    >>> y
    [[1], [1], [1], [1]]
    

    Why? Well, when you write y=[x]*4] you make a list of four names. But those are all names for the same thing! If you .append(1) to the object named by the first name, you change the object that all the names, well, name.

    Now, just as before,

    name = [[]]*4
    

    is the same as

    _temp = []
    name = [_temp, _temp, _temp, _temp]
    

    You haven’t explicitly created a new name. But you have done so implicitly! You only call [] once, so you only make one new list. You then make a list of four names, each of the new empty list you just made. So it should come as no surprise that all the names name the same thing!


    How to fix this, you say? Well, you need to call [] once for each new list that you want. To do that, use a list comprehension:

    [[] for _ in range(4)]
    

    Note that _ is just another name, although it is conventionally used for things you don’t care about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am having some trouble with a simple callback; am getting an 'expected method body'
I'm having some trouble getting my page laid out the way I want. I
I'm having some trouble getting this code to compile on Linux but it works
I'm getting better at regex, but I'm still having some trouble... I'm trying to
I'm having some trouble getting a search by zip code proximity query working. I've
I'm having some trouble getting results from a c# OleDbCommand on an MS Access
I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the
I'm having some trouble getting jQuery to play nice with DokuWiki - has anyone
I am having some trouble getting Elmah to work with url routing in an
I am having some trouble getting an interactive, animated plane created from a MovieClip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.