Extracting value from dictionary in a list - list

I have this list with dictionaries:
[{"Name": "Tore", "Score": 7}, {"Name": "Arne", "Score": 10}, {"Name": "Terje", "Score": 4}]
and want to put the information in variables so i can get this:
first = "Tore
first_number = 7
second = "Arne
second-number = 10
How do I access these values in these variables?
Tried allot of different ways, but neither works.

Related

What's the best way to sort MAP<String, Object> by value in Dart programming?

I'm new to dart programming, I came from a Javascript environment
everything is fresh to me and I really have no Idea how this sorting works
in dart.
Here's an example from my API
List example = [
{"id": "1", "age": "20"},
{"id": "2", "age": "21"},
{"id": "3", "age": "22"},
]
Before I will assign "example" to other variables eg.
var example2 = example;
I want to sort it by "age", I found libs and other "LONG" solutions out there but feels like there's another way.. Thanks in advance!
There is sort function for list. Try reading the documentation a bit:
void main() {
List example = [
{"id": "1", "age": "20"},
{"id": "2", "age": "19"},
{"id": "3", "age": "22"},
{"id": "4", "age": "9"},
];
print(example);
example.sort((x,y) => x["age"].compareTo(y["age"]));
print(example);
}
EDIT: Your definition should contain integers logically:
void main() {
List example = [
{"id": 1, "age": 20},
{"id": 2, "age": 19},
{"id": 3, "age": 22},
{"id": 4, "age": 9},
];
print(example);
example.sort((x,y) => x["age"].compareTo(y["age"]));
print(example);
}

Remove elements from List that are missing in another list

I would like to remove from List a elements that are not in another List. Suppose I have
List a = [{"id": 1}, {"id": 2}, {"id": 4}, {"id": 7},]
List b = [{"id": 1}, {"id": 2}, {"id": 7},]
Then I would like to remove from List a the element {"id": 4}, since it is missing in List b
If List b were
List b = [{"id": 1}, {"id": 2}, {"id": 4},]
then I would like to remove from List a element {"id": 7}
How to do this in Flutter.
Thank you.
void main() {
var a = [{"id": 1}, {"id": 2}, {"id": 4}, {"id": 7}];
var b = [{"id": 1}, {"id": 2}, {"id": 7}];
var c = a.map((m) => m['id']);
var d = b.map((m) => m['id']);
var e = c.toSet().difference(d.toSet());
var f = a.where((m) => e.contains(m['id']));
print(f);
}
Result
({id: 4})
I fixed it like this
List a = [{"id": 1}, {"id": 2}, {"id": 4}, {"id": 7}];
List b = [{"id": 1}, {"id": 2}, {"id": 7}];
a.removeWhere((elementA) => b.every((elementB) =>
elementB["id"] != elementA["id"]));

Data is not validated in Django Rest Framework

I am trying to pass the data to a serializer like the following:
myDict = {
"invoice_date": "2021-02-24T11:44:13+05:30",
"invoice_number": "12",
"vendor": "4",
"amount": "12",
"gst": "12",
"total_amount": "14",
"transaction_type": "Allot",
"status": "Hold",
"transactions": [
{
"t_no": 47,
"f_mile": "45",
"long_haul": "45",
"l_mile": "45",
"labour": "45",
"others": "54",
"a_t_no": 47,
},
{
"t_no": 102,
"f_mile": "12",
"long_haul": "12",
"l_mile": "21",
"labour": "21",
"others": "21",
"a_t_no": 102,
},
],
"owner": 2,
}
But when I check the validated data in the serialzer it shows it without the transactions data:
{'invoice_date': datetime.datetime(2021, 2, 24, 6, 14, 13, tzinfo=<UTC>), 'invoice_number': '12', 'amount': 12, 'gst': 12, 'total_amount': 14, 'status': 'Hold', 'transaction_type': 'Allot', 'vendor': <Vendor: Vendor object (4)>, 'owner': <User: yantraksh>}
so I tried to check the initial data that is being passed to the serializer :
<QueryDict: {
"invoice_date": ["2021-02-24T11:44:13+05:30"],
"invoice_number": ["12"],
"vendor": ["4"],
"amount": ["12"],
"gst": ["12"],
"total_amount": ["14"],
"transaction_type": ["Allot"],
"status": ["Hold"],
"transactions": [
'[{"t_no":47,"f_mile":"45","long_haul":"45","l_mile":"45","labour":"45","others":"54","a_t_no":47},{"t_no":102,"f_mile":"12","long_haul":"12","l_mile":"21","labour":"21","others": "21","a_t_no":102}]'
],
"owner": [2],
}>
It shows that the transaction data is being passed as a string, what should I change it to in order to get it as validated data ?
Based on the "evidence" in that QueryDict (namely strings wrapped in lists) it sounds like you're not submitting the data as JSON.
You can't use regular HTML form data to submit structured data, you'll need to post JSON data.

How to add a list in a list to another list

I have a list called test3 and I want to add this list to another list test0. The problem is that I only want the "noequipment" "sublist" to be added to the test0.
I tried it with the following code:
var test0 = test0 + test3["noequipment"];
This is list test3:
var test3 = [
{
"noequipment": [
{"name": "test0.1", "time": 10},
{"name": "test0.2", "time": 10},
{"name": "test0.3", "time": 10},
{"name": "test0.4", "time": 10},
]
},
{
"equipment1": [
{"name": "test1.1", "time": 10},
{"name": "test1.2", "time": 10},
{"name": "test1.3", "time": 10},
{"name": "test1.4", "time": 10},
]
},
];
Maybe you are looking for this answer
for (var i in test3[0]['noequipment']) {
test0.add(i);
print("===========>>${test0}");
}
Use code below assuming test0 is already an array
test0.addAll(test3[0]['noequipment'])
Please test this code.
test0.addAll(test3[0]["noequipment"]);
Make sure parent list is defined as var test0 = [] ;
Here I test this code https://pastebin.pl/view/ff02c9cc

How to remove duplicate items in python list

How to remove duplicate items in a list. For example spider gives me below output:
[
{"category": "Movies", "id": 4},
{"category": "Movies", "id": 5}
{"category": "Movies", "id": 4}
]
I want to remove 1st and last item because they are same. please help.
x will be your list
uniquex = []
deleted = []
for i in x:
if i in uniquex:
uniquex.remove(i)
deleted.append(i)
elif i not in deleted:
uniquex.append(i)