-3

I have a list of numbers. For example,

idList = [78,24,67,43]

from which I wish to create a dictionary of tuples like so:

files = {('file', open(78, 'rb)), ('file', open(24, 'rb)), ('file', open(67, 'rb)), ('file', open(43, 'rb))}

How can I do this?

I currently do it like this:

for each in idList:
    listofTuples.append(('file',  open(str(each) + '.pdf', 'rb')))
print(dict(listofTuples))

However, this will produce a dictionary containing only 1 tuple.

Edit: I am doing this so I can upload multiple files to a website in a single request. For example, see this answer.

jdoe
  • 634
  • 5
  • 19
  • 1
    Sorry, this is messy: `files = {('file', open(78, 'rb), ('file', open(24, 'rb), ('file', open(67, 'rb), ('file', open(43, 'rb)}`. It's not valid Python, it's not clear what you want for keys or values, it's also not clear why you want to store `open` objects in a dictionary instead of filenames. Can you please [edit](https://stackoverflow.com/posts/50277318/edit) your question with this information? – jpp May 10 '18 at 16:51
  • @jpp Thanks for pointing this out - I was missing brackets. – jdoe May 10 '18 at 18:35

1 Answers1

1

You might want to let people know you're using Python, whilst its pretty obvious it might be why you got some of these down votes.

Anyhow.

This code should produce it (not I had to drop the open(str(each)) function as I was testing this online.

Code (without open function)

idList = [78,24,67,43]
listofTuples = []
for each in idList:
    listofTuples.append(('file',  str(each) + '.pdf', 'rb'))
print(listofTuples)

Output:

[('file', '78.pdf', 'rb'), ('file', '24.pdf', 'rb'), ('file', '67.pdf', 'rb'), ('file', '43.pdf', 'rb')]

Here's your entire code below:

idList = [78,24,67,43]
listofTuples = []
for each in idList:
    listofTuples.append(('file',  open(str(each) + '.pdf', 'rb')))
print(listofTuples)

Hopefully that's what you're looking for, if not you may want to include more information in the question.

Dan
  • 958
  • 2
  • 11
  • 25