Python Forum
[solved] Variable number of dictionnaries as argument in def()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] Variable number of dictionnaries as argument in def()
#1
In the current code, I’m performing tests to works with a variable number of dictionaries; I’m looking for a way to get all keys and their values.

In the current example, especially in DictionnaryTest2 function, I fail in changing the tuple into the original list, in order to get key names (and an error is coming): I’m wondering if I’m using the right way (this is the first time) and so how to proceed?

Thanks for any advice

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np

# A dictionnary is created
n = 10
m = 1
X = np.random.random( (n,m) )
Y = np.random.random( (n,m) )
Z = np.random.random( (n,m) )
MyDictionnary = {'Abcissa': X, 'Ordinate': Y, 'Altitude': Z}
MyDictionnary2 = {'Abcissa2': X, 'Ordinate2': Y, 'Altitude2': Z, 'Theta': (X+Y)}
del X, Y, Z

# Dictionnary keys are listed / the dictionnary is explicitly expressed
KeyList0 = list(MyDictionnary.keys())
print("Key list (explicitly) : {}".format(KeyList0))


# Dictionnary keys are listed / the dictionnary name is a variable
MyVar = 'MyDictionnary'
KeyList1 = list(locals()[MyVar].keys())
print("Key list (name=variable) : {}".format(KeyList1))


# Now inside a function with the dictionnary in argument
def DictionnaryTest1(MyDict):
  NewVar = 'MyDict'
  KeyListFunction = list(locals()[NewVar].keys())
  print("Key list in a function : {}".format(KeyListFunction))
  return
  
KeyList1 = DictionnaryTest1(MyDictionnary)



# A list a dictionnary names is now created
DictionnaryNamesTables = ['MyDictionnary', 'MyDictionnary2']

# just for printing the dictionnaries list
for i in range(len(DictionnaryNamesTables)): 
  print(DictionnaryNamesTables[i])
  

def DictionnaryTest2(*args):

  # tests
  print("Type args = {}".format(type(args)))
  print("length args = {}".format(len(args)))
  print("args = {}".format(args))
  args = list(args)
  print("length args (after list())= {}".format(len(args)))
  NumberOfDictionnaries = len(args)
  
  for i in range(len(args)):
    NewVar = args[i]  #
    print("NewVar = {}".format(NewVar))
    KeyListFunction2 = list(locals()[NewVar].keys())
    print("KeyListFunction2 = {}".format(KeyListFunction2))

  return

KeyList2 = DictionnaryTest2(DictionnaryNamesTables)
Output:
Type args = <class 'tuple'> length args = 1 args = (['MyDictionnary', 'MyDictionnary2'],) length args (after list())= 1 NewVar = ['MyDictionnary', 'MyDictionnary2']
and the (logical) error
Error:
TypeError: unhashable type: 'list'
Reply


Messages In This Thread
[solved] Variable number of dictionnaries as argument in def() - by paul18fr - Apr-19-2021, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 499 Jan-27-2024, 05:23 PM
Last Post: Winfried
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,158 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  SOLVED variable as tuple name krayon70 7 2,014 Apr-09-2022, 03:30 PM
Last Post: krayon70
  Give a number for Variable quest 2 1,648 Jan-31-2022, 08:35 AM
Last Post: ibreeden
  Get latest version off website and save it as variable [SOLVED] AlphaInc 5 2,172 Nov-14-2021, 09:00 PM
Last Post: DeaD_EyE
  Adding an ascending number [SOLVED] AlphaInc 3 2,366 Jul-11-2021, 10:13 AM
Last Post: perfringo
  [solved] subdictionaries path as variable paul18fr 4 2,783 May-18-2021, 08:12 AM
Last Post: DeaD_EyE
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,440 May-02-2021, 03:45 PM
Last Post: Anldra12
  Defining an object's argument whose name is stored in a variable arbiel 2 2,305 Dec-11-2020, 10:19 PM
Last Post: arbiel
  [regex] Good way to parse variable number of items? Winfried 4 2,719 May-15-2020, 01:54 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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