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
Related
This question already has answers here:
Does python have a sorted list?
(9 answers)
What do I use for a max-heap implementation in Python?
(19 answers)
Closed 6 months ago.
I need a data structure like a dict where I can insert elements with a key and then efficiently take out largest element by value when I need it.
This question already has answers here:
Python: Short way of creating a sequential list with a prefix
(3 answers)
Create a list of strings with consecutive numbers appended
(6 answers)
Appending the same string to a list of strings in Python
(12 answers)
Closed 7 months ago.
In Python how can I need to create a long list that I'm trying to avoid typing, the list looks lis this
brands = ['_1','_2','_3'... '_998']
I can create the list of numbers with a for loop, but I'm trying to use list comprehension for the characters which should be faster.
Thanks!
my list=["_" + str(i) for i in range(1,999)]
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))
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(...)
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))