0

The following is my data:

"Jay": 1, "Raj": 1,"Jay": 3,"Raj": 3,"Jay": 10,"Raj": 2,"Jay": 2

I would like to put it into a list to make it look like this:

["Jay": 1,"Raj": 1,"Jay": 3,"Raj": 3,"Jay": 10,"Raj": 2,"Jay": 2]

My issue is that only the final element is pushed to the entire list. The result looks like this:

["Jay": 2,"Raj": 2,"Jay": 2,"Raj": 2,"Jay": 2,"Raj": 2,"Jay": 2]

How would I resolve this issue?

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
Arpit Shah
  • 45
  • 1
  • 6
  • Try using a **list of dictionary elements** and update an element like `list[][] = ` :- example `list[3]["Raj"] = 4` – lorem_bacon Jun 03 '20 at 05:25

2 Answers2

1

tou use dict it has a unique key value par properties that why when you and new it will update last add key value par if not has any key it will add new key

It will work on list of list if you do want replace last element then try following way may be your problem solve

list_var = []
list_var.append(['Jay',1])
print(list_var) #[['Jay',1]]

#you can append list using append method so list is ordered collection so add another one

list_var.append(['Raj',1])
print(list_var) #[['Jay',1],['Raj',1]]

#add another
list_var.append(['Jay',3])
print(list_var) #[['Jay',1],['Raj',1],['Jay',3]]

#using for you will print
for name,score in list_var:
    print(name,score)

#or you will access by index
list_var[0] #[['Jay',1]

Or you can manipulate by map filter reduce method

Using this stuff may you understand if any problem let me know and if it's work then let me know

l.b.vasoya
  • 1,188
  • 8
  • 23
0

Don't use dict, use list and tuple instead:

your_list = [ ('Jay', 1), ('Raj', 3), ('Jay', 1), ('Raj', 3) ]

When using dict you cannot use same keys.

lenik
  • 23,228
  • 4
  • 34
  • 43