Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to Flatten a nested list in python

import itertools

a=[[1, 2, 3], [4, 5, 6]]

flat_list = list(itertools.chain.from_iterable(a))
print(flat_list)

# Output:
[1, 2, 3, 4, 5, 6]
Comment

python nested list

# example of a nested list
my_list = [[1, 2], ["one", "two"]]

# accessing a nested list
my_list[1][0] # outputs "one"
Comment

python nested list

L = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]   
for list in L:
    for number in list:
        print(number, end=' ')
# Prints 1 2 3 4 5 6 7 8 9
Comment

how to make one list from nested list

>>> from collections import Iterable
def flatten(lis):
     for item in lis:
         if isinstance(item, Iterable) and not isinstance(item, str):
             for x in flatten(item):
                 yield x
         else:        
             yield item

>>> lis = [1,[2,2,2],4]
>>> list(flatten(lis))
[1, 2, 2, 2, 4]
>>> list(flatten([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Comment

List Nested Lists

x = [3, 4, [7, 8]]
print(x[2][1])
# 8
Comment

nested list in pthon

n=int(input())
res=[]
grade=[]
for i in range(n):
    name=input()
    mark=float(input())
    res.append([name,mark])
    grade.append(mark)   #calculation 2nd lowest
#print(res)   
#print(grade)
grade=sorted(set(grade))  #sorted unique
#print(grade)
m=grade[1]
#print(m)
name=[]
for val in res:
    if m==val[1]:
        name.append(val[0])
#print(name)   #unsorted     
name.sort()
#print(name)   #sorted
for nm in name:
    print(nm)
Comment

PREVIOUS NEXT
Code Example
Python :: how to update a python package 
Python :: how to find duplicates in pandas 
Python :: find distance between two points in python 
Python :: Adding new column to existing DataFrame in Pandas 
Python :: padding figures in pyplot 
Python :: how to import a module from a different directory in python 
Python :: python while true 
Python :: split long list into chunks of 100 
Python :: rotate 2 dimensional list python 
Python :: python libraries 
Python :: store message sent by user in string discord py 
Python :: python3 create list from string 
Python :: numpy split 
Python :: import os python 
Python :: python selenium print xpath of element 
Python :: match in python 
Python :: how to duplicate a list in python 
Python :: python list max value 
Python :: Fun & learn with python turtle 
Python :: find the range in python 
Python :: sort pandas dataframe by specific column 
Python :: how to convert one dimensional array into two dimensional array 
Python :: using comma as the thousand separator 
Python :: circular linked list in python 
Python :: python update dict if key not exist 
Python :: python loop over list 
Python :: how to convert uppercase to lowercase and vice versa in python 
Python :: use decorator in class python 
Python :: sort dict based on other list 
Python :: # get the largest number in a list and print its indexes 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =