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 8286689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:50:50+00:00 2026-06-08T11:50:50+00:00

Suppose the following toyset (from a CSV file where column names are the keys

  • 0

Suppose the following toyset (from a CSV file where column names are the “keys” and I’m only interested in some rows that I put in “data”):

keys = ['k1', 'k2', 'k3', 'k4']
data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

I want to get a dictionary with a list for each column, like this:

{'k1': [1, 5, 9, 13], 'k2': [2, 6, 10, 14], 'k3': [3, 7, 11, 15], 'k4': [4, 8, 
12, 16]}

In my code I first initialize the dictionary with empty lists and then iterate (in the order of the keys) to append each item in their list.

my_dict = dict.fromkeys(keys, [])
for row in data:
    for i, k in zip(row, keys):
        my_dict[k].append(i)

But it doesn’t work. It builds this dictionary:

{'k3': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 'k2': [1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 'k1': [1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12, 13, 14, 15, 16], 'k4': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16]}

You can see that all the elements are in all lists instead of just four elements in each list. If I print i, k in the loop it does the correct pairs of items and keys. So I guess the problem is when I add item i in the list for key k.

Does anyone know why all elements are added to all lists and what would be the right way of building my dictionary?

Thanks in advance

  • 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-06-08T11:50:53+00:00Added an answer on June 8, 2026 at 11:50 am

    zip it but transpose it first:

    >>> keys = ['k1', 'k2', 'k3', 'k4']
    >>> data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
    >>> print dict(zip(keys, zip(*data)))
    {'k3': (3, 7, 11, 15), 'k2': (2, 6, 10, 14), 'k1': (1, 5, 9, 13), 'k4': (4, 8, 12, 16)}
    

    If you want lists not tuples in the array:

    >>> print dict(zip(keys, [list(i) for i in zip(*data)]))
    

    And if you want to use your version, just make dictionary comprehension, not fromkeys:

    my_dict = { k : [] for k in keys }
    

    The problem in your case that you initialize my_dict with the same value:

    >>> my_dict = dict.fromkeys(keys, [])
    >>> my_dict
    {'k3': [], 'k2': [], 'k1': [], 'k4': []}
    >>> my_dict['k3'].append(1)
    >>> my_dict
    {'k3': [1], 'k2': [1], 'k1': [1], 'k4': [1]}
    

    When you do it right (with dictionary/list comprehension):

    >>> my_dict = dict((k, []) for k in keys )
    >>> my_dict
    {'k3': [], 'k2': [], 'k1': [], 'k4': []}
    >>> my_dict['k3'].append(1)
    >>> my_dict
    {'k3': [1], 'k2': [], 'k1': [], 'k4': []}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the following class public class Message { // some code } And a
Suppose the following event happen in chronicle order. A file get deleted on path
I am usung oracle 11g. Suppose the following query returns n rows. SELECT t.id,t.from_date,t.price
Suppose the following situation: A user buys an app this app is removed from
Suppose the following: you download an image from the internet in your ipad app
I hope some one more SQL wise can help me. Suppose the following table
What I'm trying to accomplish is the following: Suppose I have a function that
Suppose following codes: IEnumerable<MyClass> MakeQuery() { var query = from m in session.Linq<MyClass>() select
I need to clear some points regarding synchronization at block level. suppose following synchronization
Suppose the following scenario: I have a master database that contains lots of data,

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.