How could I remove adjacent duplicate value without using a loop? [duplicate] - python

This question already has answers here:
Remove adjacent duplicate elements from a list [duplicate]
(17 answers)
Closed 3 years ago.
Like the given list is
l=[4,2,3,4,4,5]
Turn into
l=[4,2,3,4,5]
I try to create an empty list to store the value but it is hard to compare if the two elements are equal with no loop

to store every element only once is same like a set.
you can use following code.
l=[2,3,4,4,5]
l=list(set(l))
l.sort()
it will give your expected output

You can use a dictionary which only keeps unique values as its key
l=[2,3,4,4,5]
list(dict.fromKeys(l))

Related

How to convert list object to string list [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 10 months ago.
I have problem with converting
[['a'],['b'],['c']] --> ['a','b','c']
it seems simple but I am struggling for hours...
You could use a list comprehension to do something as simple as
[element[0] for element in letters_list]
This remakes the list, extracting the 0-index element from each sublist

Why Python Dict is not printing the same order that inserted? [duplicate]

This question already has answers here:
Are dictionaries ordered in Python 3.6+?
(6 answers)
Closed 11 months ago.
I am using the following code to print keys and values of a dict.
for key, val in index1.items():
print("++++++++")
print ( key)
print (val)
print("++++++++")
I want it's to be printed in the same order as it was inserted. Currently, it's not following that.
It is not printing in the same order because the order is not important since you can't get any values with indexes because you use strings as keys to get values. But you can always use .sorted() if you want (it doesn't impact anything in the dictionary, just the printing part)

Append something into interior list within nested list [duplicate]

This question already has answers here:
How to insert into python nested list
(3 answers)
Closed 5 years ago.
so I have a nested list like this:
nested list=[['Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last'], ['T101,10/04/2016,55,10/01/2017,25.08'], ['T102,01/07/2016,10,15/05/2017,30.94'], ['T103,15/11/2016,94,13/06/2017,83.16'], ['T104,25/04/2017,58,10/01/2017,25.08'], ['T105,24/05/2017,5,20/06/2017,93.80']]
and I want to append another element into this list ['Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last'] ,how should I do it?
This list is originally created from a csv file and is split by its line followed by its elements
by coldspeed:
nested_list[0].append(...)

what is the difference between [(a,b),(c,d)] and [[a,b],[c,d]] and how can I turn one into another in python? [duplicate]

This question already has answers here:
What's the difference between lists enclosed by square brackets and parentheses in Python?
(7 answers)
Closed 6 years ago.
So my question is exactly defined in the title. That is, what is the difference between
[(a,b),(c,d)]
and
[[a,b],[c,d]]
and how I can turn one into another in Python.
This question is partly duplicate but the main part is how to turn one into another which in not duplicate.
A list is that which encloses its contents in a bracket [].
A tuple is that which encloses its contents in a parentheses ().
Lists are mutable (changeable); tuples are not.
Turning a list into a tuple is possible:
Changing a tuple to list:
list(myTuple)
Changing list to tuple:
tuple(myList)

Key-value map in python [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 7 years ago.
I have two list of same length l1=[reboot,revision,disk_space] and l2=[no,12.15,4300] I want to make these two list as key-value map. Output should be like this [reboot:no,revision:12.18,disk_space:4300].. I tried Zip it is not working
You can create a dictionary from the output of zip (Thanks, Martijn!):
print dict(zip(l1, l2))

Categories

Resources