Create list from iterable in python | Laptrinhcanban.com

HOME › >>

Create list from iterable in python

Posted at: https://laptrinhcanban.com/en

Instructions for creating list from iterable in python . You will learn what is the concept of iterable in python as well as how to create list from iterable in python in this lesson.

What is Iterable in python

We often encounter the **iterable** keyword when learning how to use data types with multiple elements such as *array, list, tuple, etc*. The concept of iterable is not really complicated, but it can make many new programmers headache there. Details of the iterable Kiyoshi would like to see you in another article. In the framework of this article, you can understand in the simplest way, iterables are objects with many repeating elements such as list, string, tuple. Since the iterable contains many elements, so we can take these elements and create a list from the iterable in python .

Create list from iterable in python using class

To create an iterable word list in Python, we need to use it class. This is the part related to object oriented programming that you need to master when learning about python.
Regarding the class , please make an appointment to separate you in a separate future. In this article, you can simply understand class as a built-in blueprint , you use this blueprint to create objects that fully inherit the features of the class blueprint .
And we will use this class blueprint to generate a list from the iterable in python with the following syntax:

class list([ iterable])

In there, the iterable is an argument of the class list, we can specify it as one list, or string , or tuple etc.

Specifically, we write the following:

Specifies iterable to be a list and creates list from another list in python

We assign the iterable argument as a list and create a new list from this list as shown in the following example:

ordinary_list = [ "A" , "B" , "C" ] 
new_list = list (ordinary_list);
print (new_list)

#>> ['A', 'B', 'C']

You can see that a newly created list has the same elements as the original list. Note that we copy a list to create a new list, so these two lists are two separate objects.

Specifies iterable as a string and generates list from string in python

We assign the iterable argument of the class to a string and create a new list from this string as shown in the following example:

mylist= list('cityboy')
print(mylist)

#>> ['c', 'i', 't', 'y', 'b', 'o', 'y']

You can see that a new list has been created consisting of letter-by-letter elements in the specified string.

Specify iterable as a tuple and create list from tuple in python

We assign the iterable argument of the class to a tuple and create a new list from this tuple as shown in the following example:

mylist = list(("A", "B", "C"))
print(mylist)

#>> ['A', 'B', 'C']

You can see that the original tuple is copied and created a new list.

Specifies iterable as a range and generates list from range in python

range is an object created by class range , whose elements are consecutive numbers from start to end number, specified in the class’s argument.
The syntax for specifying is rangeas follows:

class range (start, stop , step)

OR

class range(stop)

In which, the arguments of rangethe mean:

  • start : start number
  • stop : the number ends
  • step : step

We can either specify all the arguments as above, or omit and just use the arguments stopas shown below. And if the argument startis omitted, python will be implied start = 0.

More specific instructions on functions range, Kiyoshi would like to see you in another article.

For now we will create list from range in python like following example:

mylist = list (range (10))
print (mylist)
#>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

mylist = list (range (2, 10))
print (mylist)
#>> [2, 3, 4, 5, 6, 7, 8, 9]

mylist = list (range (2, 10, 2 ))
print (mylist)
#>> [2, 4, 6, 8]

You can see that a new list of elements is the specified number in the rangecreated object .

Summarize

Above, Kiyoshi showed you how to Create list from iterable in python. To better understand the lesson content, practice rewriting today’s examples.

And let’s learn more about Python in the next lessons.

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/tao-list-tu-iterable-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.