Jump to content

Making lists from list in python

Yellow_
Go to solution Solved by Imbellis,

Do you mean something like this?

list1 = [['Sam', 'Canada', 'USA'], ['John', 'Mexico'], ['Albert', 'Canada']]
Sublist = []
for i in range(len(list1)):  # Loops though List1
    Sublist = list1[i]  # Setting Sublist to individual sections of list1
    print(Sublist[1:])  # Skips first item in list

prints:

Spoiler

['Canada', 'USA']
['Mexico']
['Canada']

 

 

I have two lists, and they go like this:

 

list1 = [[Sam, Canada, USA], [John, Mexico]]

In a for loop, I want to be able to make a different list per loop of just the locations.

 

So eg. I want the first loop to give me 

[Canada, USA]

and the second to give me

[Mexico]

 

I can't figure out how to do this, as the two lists are different sizes. Can I do something like this?:

newlist = []

newlist.extend(list[1],list[len(list)])

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Do you mean something like this?

list1 = [['Sam', 'Canada', 'USA'], ['John', 'Mexico'], ['Albert', 'Canada']]
Sublist = []
for i in range(len(list1)):  # Loops though List1
    Sublist = list1[i]  # Setting Sublist to individual sections of list1
    print(Sublist[1:])  # Skips first item in list

prints:

Spoiler

['Canada', 'USA']
['Mexico']
['Canada']

 

 

Fan Comparisons          F@H          PCPartPicker         Analysis of Market Trends (Coming soon? Never? Who knows!)

Designing a mITX case. Working on aluminum prototypes.

Open for intern / part-time. Good at maths, CAD and airflow stuff. Dabbled with Python.

Please fill out this form! It helps a ton! https://linustechtips.com/main/topic/841400-the-poll-to-end-all-polls-poll/

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Imbellis said:

Do you mean something like this?


list1 = [['Sam', 'Canada', 'USA'], ['John', 'Mexico'], ['Albert', 'Canada']]
Sublist = []
for i in range(len(list1)):  # Loops though List1
    Sublist = list1[i]  # Setting Sublist to individual sections of list1
    print(Sublist[1:])  # Skips first item in list

prints:

  Hide contents

['Canada', 'USA']
['Mexico']
['Canada']

 

 

YES

 

Oh my god thanks. What solved it for me was the "1:" - I totally forgot I could do that.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×