Home > Enterprise >  Special arrays in python
Special arrays in python

Time:09-06

this is the input array

Array= [{Client: 1, Budget: 520, Name: "Oscar"},{Client: 2, Budget: 430, Name: "Jane"}]

Hi, is there anyway to access this array in that way:

print(Array[0])

=> {Client: 1, Budget: 520, Name: "Oscar"}

print(Array[0][Budget])

=> 520

I have checked with dictionaries but apparently dictionaries requires " " ie {Name: "Oscar"} should be {'Name':"Oscar"} which is not out input

CodePudding user response:

You have an inconsistency in your input data. First of all, let's consider that this is an array of dictionaries, so index access is allowed.

According to your example: Client, Budget and Name are pre-defined variables. In that case you can access but using a value behind, which in my opinion leads to a design error.

Anyway I will show you an example:

Client = 'client'
Budget = 'budget'
Name = 'name'

Array= [{Client: 1, Budget: 520, Name: "Oscar"},{Client: 2, Budget: 430, Name: "Jane"}]

Now you can access the data the way you wanted.

print(Array[0])

{Client: 1, Budget: 520, Name: "Oscar"}

print(Array[0][Budget])

520

Consider using quotes in key names to avoid unnecessary references.

  • Related