22

I have a file which contains data in the following format: please note this is an example of what it looks like, the actual file contains more than 2 rows

1    30    5
2    64    4

I read in the file, convert the text to integers, and store them into a list. This is done with the following code:

file = open("dataFile.txt", "r") 
items = [] 
for line in file:
    line = map(int,line.split()) #convert the text data to integers 
    items.append(line) #add text data to list

The current format for the list looks like:

[[1, 30, 5], [2, 64, 4]]

I need to turn my list of lists into a dictionary. How would one go about doing this?

Dictionary key should be the first element

JustAskingThings
  • 295
  • 1
  • 3
  • 10
  • 3
    What do you want the dictionary key to be? – paisanco Dec 10 '15 at 00:47
  • In your example, write what the end result should be. The only thing I can think of is `{1: [30, 5], 2: [64, 4]}`... but that has its problems, e.g. what if there are two rows starting with number 1? BTW, *why* do you want a dictionary in the first place? What is wrong with a list of lists of numbers being stored as a list of lists of numbers? – zvone Dec 10 '15 at 00:53
  • I have a feeling this will turn into a problem that needs a [namedtuple](https://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python) or [OrderedDict](https://docs.python.org/3/library/collections.html#collections.OrderedDict) – LinkBerest Dec 10 '15 at 01:05

2 Answers2

25

I'm going to play guess-what-you-want, and assume the first numbers in each row are in fact some kind of sequential identifier, and you want

1    30    5
2    64    4

to become

1  : [30, 5]
2  : [64, 4]

so...

with open("dataFile.txt") as dataFile:
    items = {}
    for line in dataFile:
        line = map(int, line.split())  #convert the text data to integers
        key, value = line[0], line[1:]
        items[key] = value

(and I've changed the name of file because file() is already a builtin function in Python, and reusing that name for something else is bad form).


Or you could use a dictionary comprehension instead, starting with your items list:

itemDict = {item[0]: item[1:] for item in items}
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
9

Assuming you want your dictionary key to be the 1st element of the list, here is an implementation:

list = [[1, 30, 5], [2, 64, 4]]
dict = {}

for l2 in list:
    dict[l2[0]] = l2[1:]

It works by iterating through list, and the sub-list l2. Then, I take the 1st element of l2 and assign it as a key to dict, and then take the rest of the elements of l2 and put it as the value.

The result is the finished dictionary {1: [30, 5], 2: [64, 4]}

Patrick Yu
  • 972
  • 1
  • 7
  • 19
  • how would you obtain something like: items = ( (1, 30, 5), (2, 64, 4) ), is this still a dictionary? – JustAskingThings Dec 10 '15 at 01:03
  • No, that is a tuple. Dictionaries have to have a key-value pair, like a hash-table. Also, what is your key in the program? First value? – Patrick Yu Dec 10 '15 at 01:04
  • how would you implement a tuple? Correct, my key is the first value which corresponds to an ID – JustAskingThings Dec 10 '15 at 01:08
  • To declare tuple, you just do `tuple = ( (1, 30, 5), (2, 64, 4) )`, but if you want to change list -> tuple, then you need a for-loop in addition to calling `tuple(list)` to convert the 2nd degree lists to tuples as well. – Patrick Yu Dec 10 '15 at 01:10
  • Currently, I could find implementaion of 1D list to tuple: http://stackoverflow.com/questions/12836128/python-convert-list-to-tuple, but sorry, I currently don't know how to implement 2D conversions :-( ... – Patrick Yu Dec 10 '15 at 01:21
  • no worries, I found it. Thanks for your help! – JustAskingThings Dec 10 '15 at 01:26