Python: Turn Nested List to Dictionary [duplicate] - python

This question already has answers here:
Get first element of sublist as dictionary key in python
(5 answers)
Closed 3 years ago.
I am trying to turn a nested list like this:
(Example list)
[['A','1','2','3'], ['B','4','5','6'],...]
To a dictionary that looks like this
{'A':'1','2','3','B': '4','5','6'}
Can someone please help me?

You can use dictionary comprehension:
lst = [['A','1','2','3'], ['B','4','5','6']]
{e[0]: e[1:] for e in lst}
This returns:
{'A': ['1', '2', '3'], 'B': ['4', '5', '6']}

Related

Python: split one element in list [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Changing one element in a list from string to integer
(1 answer)
Closed 1 year ago.
Can not solve problem i have following list ['882','200','60'] sometimes it can be ['882','200'] i want get list like this [['8','8','2'], '200', '60'].
Another words no matter how many items are in the list, we always split the first item
You can use enumerate to get the first element:
l = ['882','200','60']
[e if i else list(e) for i,e in enumerate(l)]
output:
[['8', '8', '2'], '200', '60']
other examples
>>> [e if i else list(e) for i,e in enumerate(['123'])]
[['1', '2', '3']]
>>> [e if i else list(e) for i,e in enumerate(['882','200'])]
[['8', '8', '2'], '200']
mylist = ['882','200','60']
mylist[0] = list(mylist[0])

Deleting an item in a dictionary using dictionary comprehension [duplicate]

This question already has an answer here:
python delete dict keys in list comprehension
(1 answer)
Closed 2 years ago.
How can I delet an item from a dictionary if the value of an item is a specific string, using dictionay comprehension?
You need to keep the other ones
values = {'a': '1', 'b': '2', 'c': '4', 'd': '4'}
toRemove = '4'
values = {k: v for k, v in values.items() if v != toRemove}
print(values) # {'a': '1', 'b': '2'}

Flattening list in python without flatten the individual strings in the list [duplicate]

This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
How can I flatten lists without splitting strings?
(7 answers)
Closed 4 years ago.
This is the list i am trying to flatten.
pp=['a1b0c00',['00ffbcd','c2df']]
flat_list = [item for sublist in pp for item in sublist]
flat_list: ['a', '1', 'b', '0', 'c', '0', '0', '00ffbcd', 'c2df']
Expected result is a single list:['a1b0c00','00ffbcd','c2df'] .
How to flatten this kind of lists in python?
This is one approach.
pp=['a1b0c00',['00ffbcd','c2df']]
res = []
for i in pp:
if isinstance(i, list):
res.extend(i)
else:
res.append(i)
print( res )
Output:
['a1b0c00', '00ffbcd', 'c2df']

Correct way to change a list of strings to list of ints [duplicate]

This question already has answers here:
Can't modify list elements in a loop [duplicate]
(5 answers)
Closed 6 years ago.
dict = {0: ['2', '6'], 1: ['2'], 2: ['3']}
print("Original: ")
print(dict)
for key,vals in dict.items():
vals = [int(s) for s in vals]
print("New: ")
print(dict)
Output:
Original:
{0: ['2', '6'], 1: ['2'], 2: ['3']}
New:
{0: ['2', '6'], 1: ['2'], 2: ['3']}
I can't figure why the list of values is not changing, I have tried the map() function and it also does not work, any reason why?
In Python 3:
dict = {k: list(map(int, v)) for k, v in dict.items()}
Because you don't overwrite actually values in your dictionary. Try to do:
for key,vals in dict.items():
dict[key] = [int(s) for s in vals]
With dict comprehensions it looks much better, actually. I just tried to show what should be changed in your code.

Combine elements in a nested list - Python 3 [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python3 Concatenating lists within lists
I have
Nested_List = [['John', 'Smith'], ['1', '2', '3', '4']]
I want to manipulate this list such that I get this output:
Nested_List = [['John Smith'], ['1', '2', '3', '4']]
Basically the two elements in the first list were combined. How do I do this?
newElement = [' '.join(NestedList[0])]
Nested_List[0] = newElement
(modifies list, bad practice, but simple)

Categories

Resources