Jump to content

Mixed Lists In Python

beach_boy98

So for my python homework we had to find out how to get an item out of a mixed lists. The example he gave us looked something like this. : List = [6, ,4 bacon, 'Hi'] I need to know how to get the items out of it.

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

So for my python homework we had to find out how to get an item out of a mixed lists. The example he gave us looked something like this. : List = [6, ,4 bacon, 'Hi'] I need to know how to get the items out of it.

bacon = "Bacon"List = [6, 4, bacon, "Hi"]print List[0]

This will print the first item of the list (6), if you replace the 0 on the last line with a 1 you will get the second item from the list (4), etc...

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

Can you be a bit more specific with what you need to get?  You can always do a simple if-statement to see if an item is in the list, and return it if it is:

foo = 'blah'my_list = [1, 4, 'blah', 4.5]if foo in my_list:    do stuff

Or just do normal list indexing.  I think list indexing is a bit faster (not really important unless you need to do this a lot), but the if foo in my_list approach is a bit better if you already know what specific item in a list you're looking for.  Either way, mixed lists aren't particularly special in Python, since the whole list doesn't have a single type (unlike, say, C, where you declare a type for a whole array).  So anythin you can do with a normal list of, say, all integers, you can do with a mixed list.  Indexing, slicing, etc all have the same syntax/implementation.

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

×