Python Forum
convert List with dictionaries to a single dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert List with dictionaries to a single dictionary
#1
Hey guys,

i have a matrix with lots of zeroes. In order to make the code more efficient i want to write code, that takes the rows and colums as indexes and their values as a dictionary.

I managed to do the task but i could not figure out how to make a dictionary out of a list with dictionaries.

heres the Code:
matrix = [[0, 0, 0, 1, 0],
         [0, 0, 0, 0, 0],
         [0, 2, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 3, 0]]
dictionary = []

row = -1    # to make row start at 0
for i in matrix:  # row
    row += 1
    column = -1
    for j in i:   # column
        column += 1
        if j != 0:
            dictionary.append({(row, column): j})
print(dictionary)
the output is:
[{(0, 3): 1}, {(2, 1): 2}, {(4, 3): 3}]

thats a list containing dicts. But i want a a single dictionary with with tuples as keys containing row and column indexes and the matrix elements as values.

Thanks Heart
Reply
#2
(Jan-22-2021, 02:28 PM)iamaghost Wrote: Hey guys,

i have a matrix with lots of zeroes. In order to make the code more efficient i want to write code, that takes the rows and colums as indexes and their values as a dictionary.

I managed to do the task but i could not figure out how to make a dictionary out of a list with dictionaries.

heres the Code:
matrix = [[0, 0, 0, 1, 0],
         [0, 0, 0, 0, 0],
         [0, 2, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 3, 0]]
dictionary = []

row = -1    # to make row start at 0
for i in matrix:  # row
    row += 1
    column = -1
    for j in i:   # column
        column += 1
        if j != 0:
            dictionary.append({(row, column): j})
print(dictionary)
the output is:
[{(0, 3): 1}, {(2, 1): 2}, {(4, 3): 3}]

thats a list containing dicts. But i want a a single dictionary with with tuples as keys containing row and column indexes and the matrix elements as values.

Thanks Heart

Why do you use a list called dictionary? That doesn't turn it into a dictionary. Use a dictionary instead:
matrix = [[0, 0, 0, 1, 0],
         [0, 0, 0, 0, 0],
         [0, 2, 0, 0, 0],
         [0, 0, 0, 0, 0],
         [0, 0, 0, 3, 0]]
dictionary = dict()
 
row = -1    # to make row start at 0
for i in matrix:  # row
    row += 1
    column = -1
    for j in i:   # column
        column += 1
        if j != 0:
            dictionary[(row, column)]= j
print(dictionary)
iamaghost likes this post
Reply
#3
Are you familiar with sparse matrices? I don't know if this fits in well with what you are trying to do.

https://cmdlinetips.com/2018/03/sparse-m...ith-scipy/
Reply
#4
(Jan-22-2021, 03:39 PM)deanhystad Wrote: Are you familiar with sparse matrices? I don't know if this fits in well with what you are trying to do.

https://cmdlinetips.com/2018/03/sparse-m...ith-scipy/

Yea so my matrix in this example is a sparse matrix. But since i dont need those zeroes there i wanted describe a sparse matrix in form of a dict.
The reply of @Serafim helped me out :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 169 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Convert each element of a list to a string for processing tester_V 6 994 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 677 Jun-03-2021, 06:37 AM
Last Post: menator01
  function that returns a list of dictionaries nostradamus64 2 536 May-06-2021, 09:58 PM
Last Post: nostradamus64
Question convert unlabeled list of tuples to json (string) masterAndreas 4 2,133 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Parse String between 2 Delimiters and add as single list items lastyle 5 936 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Python dictionary with values as list to CSV Sritej26 4 910 Mar-27-2021, 05:53 PM
Last Post: Sritej26
Star Convert Bytearray into List using list() Shlok 2 732 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 789 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
Sad Convert python list to dictionary akanowhere 6 1,185 Dec-27-2020, 09:26 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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